Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
Cada valor tem um tipo associado, que define atributos como a quantidade de espaço alocada ao valor, o intervalo de valores possíveis que ele pode ter e os membros que ele disponibiliza. Muitos valores podem ser expressos como mais de um tipo. Por exemplo, o valor 4 pode ser expresso como um inteiro ou um valor de ponto flutuante. A conversão de tipo cria um valor em um novo tipo equivalente ao valor de um tipo antigo, mas não necessariamente preserva a identidade (ou o valor exato) do objeto original.
O .NET dá suporte automaticamente às seguintes conversões:
Conversão de uma classe derivada para uma classe base. Isso significa, por exemplo, que uma instância de qualquer classe ou estrutura pode ser convertida em uma Object instância. Essa conversão não requer um operador de casting ou de conversão.
Conversão de uma classe base de volta para a classe derivada original. Em C#, essa conversão requer um operador de conversão. No Visual Basic, ele requer o
CType
operador seOption Strict
estiver ativado.Conversão de um tipo que implementa uma interface para um objeto de interface que representa essa interface. Essa conversão não requer um operador de casting ou de conversão.
Conversão de um objeto de interface de volta para o tipo original que implementa essa interface. Em C#, essa conversão requer um operador de conversão. No Visual Basic, ele requer o
CType
operador seOption Strict
estiver ativado.
Além dessas conversões automáticas, o .NET fornece vários recursos que dão suporte à conversão de tipo personalizado. Estes incluem o seguinte:
O operador
Implicit
, que define as conversões de ampliação disponíveis entre tipos. Para obter mais informações, consulte a seção Conversão Implícita com o Operador Implícito.O
Explicit
operador, que define as conversões de restrição disponíveis entre tipos. Para obter mais informações, consulte a seção Conversão Explícita com o Operador Explícito.A IConvertible interface, que define conversões para cada um dos tipos de dados base do .NET. Para obter mais informações, consulte a seção Interface IConvertible .
A Convert classe, que fornece um conjunto de métodos que implementam os métodos na IConvertible interface. Para obter mais informações, consulte a seção Converter Classe .
A TypeConverter classe, que é uma classe base que pode ser estendida para dar suporte à conversão de um tipo especificado para qualquer outro tipo. Para obter mais informações, consulte a seção Classe TypeConverter .
Conversão implícita com o operador implícito
As conversões de ampliação envolvem a criação de um novo valor do valor de um tipo existente que tem um intervalo mais restritivo ou uma lista de membros mais restrita que o tipo de destino. A ampliação de conversões não pode resultar em perda de dados (embora possam resultar em uma perda de precisão). Como os dados não podem ser perdidos, os compiladores podem lidar com a conversão de forma implícita ou transparente, sem exigir o uso de um método de conversão explícito ou de um operador de conversão.
Observação
Embora o código que executa uma conversão implícita possa chamar um método de conversão ou usar um operador de conversão, seu uso não é exigido por compiladores que dão suporte a conversões implícitas.
Por exemplo, o Decimal tipo dá suporte a conversões implícitas de Byte, , Char, Int16, Int32, Int64, SByte, , UInt16, , UInt32e UInt64 valores. O exemplo a seguir ilustra algumas dessas conversões implícitas na atribuição de valores a uma Decimal variável.
byte byteValue = 16;
short shortValue = -1024;
int intValue = -1034000;
long longValue = 1152921504606846976;
ulong ulongValue = UInt64.MaxValue;
decimal decimalValue;
decimalValue = byteValue;
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
byteValue.GetType().Name, decimalValue);
decimalValue = shortValue;
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
shortValue.GetType().Name, decimalValue);
decimalValue = intValue;
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
intValue.GetType().Name, decimalValue);
decimalValue = longValue;
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
longValue.GetType().Name, decimalValue);
decimalValue = ulongValue;
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
ulongValue.GetType().Name, decimalValue);
// The example displays the following output:
// After assigning a Byte value, the Decimal value is 16.
// After assigning a Int16 value, the Decimal value is -1024.
// After assigning a Int32 value, the Decimal value is -1034000.
// After assigning a Int64 value, the Decimal value is 1152921504606846976.
// After assigning a UInt64 value, the Decimal value is 18446744073709551615.
Dim byteValue As Byte = 16
Dim shortValue As Short = -1024
Dim intValue As Integer = -1034000
Dim longValue As Long = CLng(1024 ^ 6)
Dim ulongValue As ULong = ULong.MaxValue
Dim decimalValue As Decimal
decimalValue = byteValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
byteValue.GetType().Name, decimalValue)
decimalValue = shortValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
shortValue.GetType().Name, decimalValue)
decimalValue = intValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
intValue.GetType().Name, decimalValue)
decimalValue = longValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
longValue.GetType().Name, decimalValue)
decimalValue = ulongValue
Console.WriteLine("After assigning a {0} value, the Decimal value is {1}.",
ulongValue.GetType().Name, decimalValue)
' The example displays the following output:
' After assigning a Byte value, the Decimal value is 16.
' After assigning a Int16 value, the Decimal value is -1024.
' After assigning a Int32 value, the Decimal value is -1034000.
' After assigning a Int64 value, the Decimal value is 1152921504606846976.
' After assigning a UInt64 value, the Decimal value is 18446744073709551615.
Se um compilador de linguagem específico der suporte a operadores personalizados, você também poderá definir conversões implícitas em seus próprios tipos personalizados. O exemplo a seguir oferece uma implementação parcial de um tipo de dados de byte assinado chamado ByteWithSign
que usa a representação de sinal e magnitude. Ele dá suporte à conversão implícita de Byte valores e SByte valores em ByteWithSign
valores.
public struct ByteWithSign
{
private SByte signValue;
private Byte value;
public static implicit operator ByteWithSign(SByte value)
{
ByteWithSign newValue;
newValue.signValue = (SByte)Math.Sign(value);
newValue.value = (byte)Math.Abs(value);
return newValue;
}
public static implicit operator ByteWithSign(Byte value)
{
ByteWithSign newValue;
newValue.signValue = 1;
newValue.value = value;
return newValue;
}
public override string ToString()
{
return (signValue * value).ToString();
}
}
Public Structure ImplicitByteWithSign
Private signValue As SByte
Private value As Byte
Public Overloads Shared Widening Operator CType(value As SByte) As ImplicitByteWithSign
Dim newValue As ImplicitByteWithSign
newValue.signValue = CSByte(Math.Sign(value))
newValue.value = CByte(Math.Abs(value))
Return newValue
End Operator
Public Overloads Shared Widening Operator CType(value As Byte) As ImplicitByteWithSign
Dim NewValue As ImplicitByteWithSign
newValue.signValue = 1
newValue.value = value
Return newValue
End Operator
Public Overrides Function ToString() As String
Return (signValue * value).ToString()
End Function
End Structure
O código do cliente pode declarar uma ByteWithSign
variável e atribuí-la Byte e SByte valores sem executar conversões explícitas ou usar operadores de conversão, como mostra o exemplo a seguir.
SByte sbyteValue = -120;
ByteWithSign value = sbyteValue;
Console.WriteLine(value);
value = Byte.MaxValue;
Console.WriteLine(value);
// The example displays the following output:
// -120
// 255
Dim sbyteValue As SByte = -120
Dim value As ImplicitByteWithSign = sbyteValue
Console.WriteLine(value.ToString())
value = Byte.MaxValue
Console.WriteLine(value.ToString())
' The example displays the following output:
' -120
' 255
Conversão explícita com o operador explícito
As conversões de redução envolvem a criação de um novo valor com o valor de um tipo existente que tem um intervalo mais amplo ou uma lista de membros maior do que o tipo de destino. Como uma conversão de redução pode resultar em uma perda de dados, os compiladores geralmente exigem que a conversão se torne explícita por meio da chamada de um método de conversão ou de um operador de conversão. Ou seja, a conversão deve ser tratada explicitamente no código do desenvolvedor.
Observação
A finalidade principal de requerer um método de conversão ou de conversão do operador para reduzir conversões é fazer com que o desenvolvedor saiba sobre a possibilidade de perda de dados ou de OverflowException para que ela possa ser tratada no código. No entanto, alguns compiladores podem relaxar esse requisito. Por exemplo, no Visual Basic, se Option Strict
estiver desativado (sua configuração padrão), o compilador do Visual Basic tentará executar conversões de restrição implicitamente.
Por exemplo, os tipos de dados UInt32, Int64 e UInt64 têm intervalos que excedem os do tipo de dados Int32, como mostra a tabela a seguir.
Tipo | Comparação com o intervalo de Int32 |
---|---|
Int64 | Int64.MaxValue é maior que Int32.MaxValue, e Int64.MinValue é menor que (tem um intervalo negativo maior que) Int32.MinValue. |
UInt32 | UInt32.MaxValue é maior que Int32.MaxValue. |
UInt64 | UInt64.MaxValue é maior que Int32.MaxValue. |
Para lidar com essas conversões de restrição, o .NET permite que os tipos definam um Explicit
operador. Os compiladores de linguagem individuais podem implementar esse operador usando sua própria sintaxe ou um membro da Convert classe pode ser chamado para executar a conversão. (Para obter mais informações sobre a Convert classe, consulte a classe Converter mais adiante neste tópico.) O exemplo a seguir ilustra o uso de recursos de linguagem para lidar com a conversão explícita desses valores inteiros potencialmente fora do intervalo em Int32 valores.
long number1 = int.MaxValue + 20L;
uint number2 = int.MaxValue - 1000;
ulong number3 = int.MaxValue;
int intNumber;
try
{
intNumber = checked((int)number1);
Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
number1.GetType().Name, intNumber);
}
catch (OverflowException)
{
if (number1 > int.MaxValue)
Console.WriteLine($"Conversion failed: {number1} exceeds {int.MaxValue}.");
else
Console.WriteLine($"Conversion failed: {number1} is less than {int.MinValue}.");
}
try
{
intNumber = checked((int)number2);
Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
number2.GetType().Name, intNumber);
}
catch (OverflowException)
{
Console.WriteLine($"Conversion failed: {number2} exceeds {int.MaxValue}.");
}
try
{
intNumber = checked((int)number3);
Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
number3.GetType().Name, intNumber);
}
catch (OverflowException)
{
Console.WriteLine($"Conversion failed: {number1} exceeds {int.MaxValue}.");
}
// The example displays the following output:
// Conversion failed: 2147483667 exceeds 2147483647.
// After assigning a UInt32 value, the Integer value is 2147482647.
// After assigning a UInt64 value, the Integer value is 2147483647.
Dim number1 As Long = Integer.MaxValue + 20L
Dim number2 As UInteger = Integer.MaxValue - 1000
Dim number3 As ULong = Integer.MaxValue
Dim intNumber As Integer
Try
intNumber = CInt(number1)
Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
number1.GetType().Name, intNumber)
Catch e As OverflowException
If number1 > Integer.MaxValue Then
Console.WriteLine("Conversion failed: {0} exceeds {1}.",
number1, Integer.MaxValue)
Else
Console.WriteLine("Conversion failed: {0} is less than {1}.\n",
number1, Integer.MinValue)
End If
End Try
Try
intNumber = CInt(number2)
Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
number2.GetType().Name, intNumber)
Catch e As OverflowException
Console.WriteLine("Conversion failed: {0} exceeds {1}.",
number2, Integer.MaxValue)
End Try
Try
intNumber = CInt(number3)
Console.WriteLine("After assigning a {0} value, the Integer value is {1}.",
number3.GetType().Name, intNumber)
Catch e As OverflowException
Console.WriteLine("Conversion failed: {0} exceeds {1}.",
number1, Integer.MaxValue)
End Try
' The example displays the following output:
' Conversion failed: 2147483667 exceeds 2147483647.
' After assigning a UInt32 value, the Integer value is 2147482647.
' After assigning a UInt64 value, the Integer value is 2147483647.
Conversões explícitas podem produzir resultados diferentes em diferentes idiomas, e esses resultados podem ser diferentes do valor retornado pelo método correspondente Convert . Por exemplo, se o valor 12,63251 de Double for convertido em Int32, o método CInt
do Visual Basic e o método Convert.ToInt32(Double) do .NET arredondam o valor de Double para retornar um valor 13, mas o operador (int)
do C# trunca Double para retornar um valor 12. Da mesma forma, o operador C# (int)
não dá suporte à conversão booliana para inteiro, mas o método Visual Basic CInt
converte um valor de true
-1. Por outro lado, Convert.ToInt32(Boolean) método converte um valor de true
para 1.
A maioria dos compiladores permite que conversões explícitas sejam executadas de maneira marcada ou desmarcada. Quando uma conversão marcada é executada, uma OverflowException é gerada quando o valor do tipo a ser convertido está fora do intervalo do tipo de destino. Quando uma conversão não verificada é executada nas mesmas condições, a conversão pode não gerar uma exceção, mas o comportamento exato se torna indefinido e um valor incorreto pode ser gerado.
Observação
Em C#, as conversões verificadas podem ser executadas usando a checked
palavra-chave junto com um operador de conversão ou especificando a opção do /checked+
compilador. Por outro lado, conversões não verificadas podem ser executadas usando a palavra-chave unchecked
junto com o operador de conversão ou especificando a opção /checked-
do compilador. Por padrão, as conversões explícitas são desmarcadas. No Visual Basic, as conversões verificadas podem ser executadas desmarcando-se a caixa de seleção Remover Verificação de Estouro de Inteiro na caixa de diálogo Configurações Avançadas do Compilador do projeto ou ao especificar a opção /removeintchecks-
do compilador. Do mesmo modo, as conversões não verificadas podem ser executadas desmarcando-se a caixa de seleção Remover Verificação de Estouro de Inteiro na caixa de diálogo Configurações Avançadas do Compilador do projeto ou ao especificar a opção /removeintchecks+
do compilador. Por padrão, as conversões explícitas são verificadas.
O exemplo de C# a seguir usa as palavras-chave checked
e unchecked
para ilustrar a diferença de comportamento quando um valor fora do intervalo de um Byte é convertido para um Byte. A conversão marcada gera uma exceção, mas a conversão não verificada atribui Byte.MaxValue à variável Byte.
int largeValue = Int32.MaxValue;
byte newValue;
try
{
newValue = unchecked((byte)largeValue);
Console.WriteLine($"Converted the {largeValue.GetType().Name} value {largeValue} to the {newValue.GetType().Name} value {newValue}.");
}
catch (OverflowException)
{
Console.WriteLine($"{largeValue} is outside the range of the Byte data type.");
}
try
{
newValue = checked((byte)largeValue);
Console.WriteLine($"Converted the {largeValue.GetType().Name} value {largeValue} to the {newValue.GetType().Name} value {newValue}.");
}
catch (OverflowException)
{
Console.WriteLine($"{largeValue} is outside the range of the Byte data type.");
}
// The example displays the following output:
// Converted the Int32 value 2147483647 to the Byte value 255.
// 2147483647 is outside the range of the Byte data type.
Se um compilador de linguagem específico der suporte a operadores sobrecarregados personalizados, você também poderá definir conversões explícitas em seus próprios tipos personalizados. O exemplo a seguir oferece uma implementação parcial de um tipo de dados de byte assinado chamado ByteWithSign
que usa a representação de sinal e magnitude. Ele dá suporte à conversão explícita de Int32 valores e UInt32 valores em ByteWithSign
valores.
public struct ByteWithSignE
{
private SByte signValue;
private Byte value;
private const byte MaxValue = byte.MaxValue;
private const int MinValue = -1 * byte.MaxValue;
public static explicit operator ByteWithSignE(int value)
{
// Check for overflow.
if (value > ByteWithSignE.MaxValue || value < ByteWithSignE.MinValue)
throw new OverflowException(String.Format("'{0}' is out of range of the ByteWithSignE data type.",
value));
ByteWithSignE newValue;
newValue.signValue = (SByte)Math.Sign(value);
newValue.value = (byte)Math.Abs(value);
return newValue;
}
public static explicit operator ByteWithSignE(uint value)
{
if (value > ByteWithSignE.MaxValue)
throw new OverflowException(String.Format("'{0}' is out of range of the ByteWithSignE data type.",
value));
ByteWithSignE newValue;
newValue.signValue = 1;
newValue.value = (byte)value;
return newValue;
}
public override string ToString()
{
return (signValue * value).ToString();
}
}
Public Structure ByteWithSign
Private signValue As SByte
Private value As Byte
Private Const MaxValue As Byte = Byte.MaxValue
Private Const MinValue As Integer = -1 * Byte.MaxValue
Public Overloads Shared Narrowing Operator CType(value As Integer) As ByteWithSign
' Check for overflow.
If value > ByteWithSign.MaxValue Or value < ByteWithSign.MinValue Then
Throw New OverflowException(String.Format("'{0}' is out of range of the ByteWithSign data type.", value))
End If
Dim newValue As ByteWithSign
newValue.signValue = CSByte(Math.Sign(value))
newValue.value = CByte(Math.Abs(value))
Return newValue
End Operator
Public Overloads Shared Narrowing Operator CType(value As UInteger) As ByteWithSign
If value > ByteWithSign.MaxValue Then
Throw New OverflowException(String.Format("'{0}' is out of range of the ByteWithSign data type.", value))
End If
Dim NewValue As ByteWithSign
newValue.signValue = 1
newValue.value = CByte(value)
Return newValue
End Operator
Public Overrides Function ToString() As String
Return (signValue * value).ToString()
End Function
End Structure
O código do cliente pode declarar uma variável ByteWithSign
e atribuir a ela os valores Int32 e UInt32 se as atribuições incluírem um operador de conversão ou um método de conversão, como mostra o exemplo a seguir.
ByteWithSignE value;
try
{
int intValue = -120;
value = (ByteWithSignE)intValue;
Console.WriteLine(value);
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
try
{
uint uintValue = 1024;
value = (ByteWithSignE)uintValue;
Console.WriteLine(value);
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
// The example displays the following output:
// -120
// '1024' is out of range of the ByteWithSignE data type.
Dim value As ByteWithSign
Try
Dim intValue As Integer = -120
value = CType(intValue, ByteWithSign)
Console.WriteLine(value)
Catch e As OverflowException
Console.WriteLine(e.Message)
End Try
Try
Dim uintValue As UInteger = 1024
value = CType(uintValue, ByteWithSign)
Console.WriteLine(value)
Catch e As OverflowException
Console.WriteLine(e.Message)
End Try
' The example displays the following output:
' -120
' '1024' is out of range of the ByteWithSign data type.
A interface IConvertible
Para oferecer suporte à conversão de qualquer tipo em um tipo base do Common Language Runtime, o .NET oferece a interface IConvertible. O tipo de implementação é necessário para fornecer o seguinte:
Um método que retorna o TypeCode do tipo de implementação.
Métodos para converter o tipo de implementação em cada tipo de base do Common Language Runtime (Boolean, Byte, DateTime, Decimal, Double e assim por diante).
Um método de conversão generalizado para converter uma instância do tipo de implementação em outro tipo especificado. As conversões sem suporte devem gerar uma InvalidCastException.
Cada tipo base do common language runtime (ou seja, Boolean, Byte, Char, DateTime, Decimal, Double, Int16, Int32, Int64, SByte, Single, String, UInt16, UInt32 e UInt64), bem como os tipos DBNull e Enum, implementam a interface IConvertible. No entanto, essas são implementações de interface explícitas; o método de conversão só pode ser chamado por meio de uma IConvertible variável de interface, como mostra o exemplo a seguir. Este exemplo converte um Int32 valor em seu valor equivalente Char .
int codePoint = 1067;
IConvertible iConv = codePoint;
char ch = iConv.ToChar(null);
Console.WriteLine($"Converted {codePoint} to {ch}.");
Dim codePoint As Integer = 1067
Dim iConv As IConvertible = codePoint
Dim ch As Char = iConv.ToChar(Nothing)
Console.WriteLine("Converted {0} to {1}.", codePoint, ch)
O requisito de chamar o método de conversão em sua interface, em vez de no tipo de implementação, torna as implementações de interface explícitas relativamente caras. Em vez disso, recomendamos chamar o membro apropriado da classe Convert para fazer a conversão entre tipos base do Common Language Runtime. Para obter mais informações, consulte a próxima seção, A Classe Converter.
Observação
Além da IConvertible interface e da Convert classe fornecida pelo .NET, idiomas individuais também podem fornecer maneiras de executar conversões. Por exemplo, o C# usa operadores de conversão; O Visual Basic usa funções de conversão implementadas pelo compilador, como CType
, CInt
e DirectCast
.
Na maioria das vezes, a IConvertible interface foi projetada para dar suporte à conversão entre os tipos base no .NET. No entanto, a interface também pode ser implementada por um tipo personalizado para facilitar a conversão desse tipo para outros tipos personalizados. Para obter mais informações, consulte a seção Conversões Personalizadas com o Método ChangeType posteriormente neste tópico.
A classe Convert
Embora a implementação da interface de IConvertible de cada tipo base possa ser chamada para executar uma conversão de tipo, chamar os métodos da classe System.Convert é a maneira recomendada, neutra em relação à linguagem, de converter de um tipo base para outro. Além disso, o Convert.ChangeType(Object, Type, IFormatProvider) método pode ser usado para converter de um tipo personalizado especificado para outro tipo.
Conversões entre tipos base
A Convert classe fornece uma maneira independente de linguagem para executar conversões entre tipos base e está disponível para todas as linguagens que visam o ambiente de execução comum. Ele fornece um conjunto completo de métodos para conversões de ampliação e restrição e lança um InvalidCastException para conversões que não têm suporte (como a conversão de um valor DateTime para um valor inteiro). As conversões de redução são executadas em um contexto verificado, e uma OverflowException será gerada se a conversão falhar.
Importante
Como a classe Convert inclui métodos para converter de/para cada tipo base, ela elimina a necessidade de chamar a implementação de interface explicita IConvertible de cada tipo base.
O exemplo a seguir ilustra o uso da System.Convert classe para executar várias conversões de ampliação e estreitamento entre tipos base do .NET.
// Convert an Int32 value to a Decimal (a widening conversion).
int integralValue = 12534;
decimal decimalValue = Convert.ToDecimal(integralValue);
Console.WriteLine($"Converted the {integralValue.GetType().Name} value {integralValue} to " +
"the {decimalValue.GetType().Name} value {decimalValue:N2}.");
// Convert a Byte value to an Int32 value (a widening conversion).
byte byteValue = Byte.MaxValue;
int integralValue2 = Convert.ToInt32(byteValue);
Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to " +
"the {integralValue2.GetType().Name} value {integralValue2:G}.");
// Convert a Double value to an Int32 value (a narrowing conversion).
double doubleValue = 16.32513e12;
try
{
long longValue = Convert.ToInt64(doubleValue);
Console.WriteLine($"Converted the {doubleValue.GetType().Name} value {doubleValue:E} to " +
"the {longValue.GetType().Name} value {longValue:N0}.");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert the {doubleValue.GetType().Name:E} value {doubleValue}.");
}
// Convert a signed byte to a byte (a narrowing conversion).
sbyte sbyteValue = -16;
try
{
byte byteValue2 = Convert.ToByte(sbyteValue);
Console.WriteLine($"Converted the {sbyteValue.GetType().Name} value {sbyteValue} to " +
"the {byteValue2.GetType().Name} value {byteValue2:G}.");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert the {sbyteValue.GetType().Name} value {sbyteValue}.");
}
// The example displays the following output:
// Converted the Int32 value 12534 to the Decimal value 12,534.00.
// Converted the Byte value 255 to the Int32 value 255.
// Converted the Double value 1.632513E+013 to the Int64 value 16,325,130,000,000.
// Unable to convert the SByte value -16.
' Convert an Int32 value to a Decimal (a widening conversion).
Dim integralValue As Integer = 12534
Dim decimalValue As Decimal = Convert.ToDecimal(integralValue)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3:N2}.",
integralValue.GetType().Name,
integralValue,
decimalValue.GetType().Name,
decimalValue)
' Convert a Byte value to an Int32 value (a widening conversion).
Dim byteValue As Byte = Byte.MaxValue
Dim integralValue2 As Integer = Convert.ToInt32(byteValue)
Console.WriteLine("Converted the {0} value {1} to " +
"the {2} value {3:G}.",
byteValue.GetType().Name,
byteValue,
integralValue2.GetType().Name,
integralValue2)
' Convert a Double value to an Int32 value (a narrowing conversion).
Dim doubleValue As Double = 16.32513e12
Try
Dim longValue As Long = Convert.ToInt64(doubleValue)
Console.WriteLine("Converted the {0} value {1:E} to " +
"the {2} value {3:N0}.",
doubleValue.GetType().Name,
doubleValue,
longValue.GetType().Name,
longValue)
Catch e As OverflowException
Console.WriteLine("Unable to convert the {0:E} value {1}.",
doubleValue.GetType().Name, doubleValue)
End Try
' Convert a signed byte to a byte (a narrowing conversion).
Dim sbyteValue As SByte = -16
Try
Dim byteValue2 As Byte = Convert.ToByte(sbyteValue)
Console.WriteLine("Converted the {0} value {1} to " +
"the {2} value {3:G}.",
sbyteValue.GetType().Name,
sbyteValue,
byteValue2.GetType().Name,
byteValue2)
Catch e As OverflowException
Console.WriteLine("Unable to convert the {0} value {1}.",
sbyteValue.GetType().Name, sbyteValue)
End Try
' The example displays the following output:
' Converted the Int32 value 12534 to the Decimal value 12,534.00.
' Converted the Byte value 255 to the Int32 value 255.
' Converted the Double value 1.632513E+013 to the Int64 value 16,325,130,000,000.
' Unable to convert the SByte value -16.
Em alguns casos, particularmente na conversão de/para valores de ponto flutuante, uma conversão pode envolver uma perda de precisão, mesmo que não gere uma OverflowException. O exemplo a seguir ilustra essa perda de precisão. No primeiro caso, um Decimal valor tem menos precisão (menos dígitos significativos) quando é convertido em um Double. No segundo caso, um Double valor é arredondado de 42,72 para 43 para concluir a conversão.
double doubleValue;
// Convert a Double to a Decimal.
decimal decimalValue = 13956810.96702888123451471211m;
doubleValue = Convert.ToDouble(decimalValue);
Console.WriteLine($"{decimalValue} converted to {doubleValue}.");
doubleValue = 42.72;
try
{
int integerValue = Convert.ToInt32(doubleValue);
Console.WriteLine($"{doubleValue} converted to {integerValue}.");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert {doubleValue} to an integer.");
}
// The example displays the following output:
// 13956810.96702888123451471211 converted to 13956810.9670289.
// 42.72 converted to 43.
Dim doubleValue As Double
' Convert a Double to a Decimal.
Dim decimalValue As Decimal = 13956810.96702888123451471211d
doubleValue = Convert.ToDouble(decimalValue)
Console.WriteLine("{0} converted to {1}.", decimalValue, doubleValue)
doubleValue = 42.72
Try
Dim integerValue As Integer = Convert.ToInt32(doubleValue)
Console.WriteLine("{0} converted to {1}.",
doubleValue, integerValue)
Catch e As OverflowException
Console.WriteLine("Unable to convert {0} to an integer.",
doubleValue)
End Try
' The example displays the following output:
' 13956810.96702888123451471211 converted to 13956810.9670289.
' 42.72 converted to 43.
Para obter uma tabela que lista as conversões de ampliação e restrição compatíveis com a Convert classe, consulte Tabelas de Conversão de Tipo.
Conversões personalizadas com o método ChangeType
Além de dar suporte a conversões em cada um dos tipos base, a Convert classe pode ser usada para converter um tipo personalizado em um ou mais tipos predefinidos. Essa conversão é executada pelo método Convert.ChangeType(Object, Type, IFormatProvider), que por sua vez encapsula uma chamada para o método IConvertible.ToType do parâmetro value
. Isso significa que o objeto representado pelo value
parâmetro deve fornecer uma implementação da IConvertible interface.
Observação
Como os métodos Convert.ChangeType(Object, Type) e Convert.ChangeType(Object, Type, IFormatProvider) usam um objeto Type para especificar o tipo de destino ao qual value
é convertido, eles podem ser usados para executar uma conversão dinâmica em um objeto cujo tipo não é conhecido em tempo de compilação. No entanto, observe que a implementação IConvertible de value
deve oferecer suporte a essa conversão.
O exemplo a seguir ilustra uma possível implementação da IConvertible interface que permite que um TemperatureCelsius
objeto seja convertido em um TemperatureFahrenheit
objeto e vice-versa. O exemplo define uma classe base, Temperature
que implementa a IConvertible interface e substitui o Object.ToString método. As classes derivadas TemperatureCelsius
e TemperatureFahrenheit
sobrepõem os métodos ToType
e ToString
da classe base.
using System;
public abstract class Temperature : IConvertible
{
protected decimal temp;
public Temperature(decimal temperature)
{
this.temp = temperature;
}
public decimal Value
{
get { return this.temp; }
set { this.temp = value; }
}
public override string ToString()
{
return temp.ToString(null as IFormatProvider) + "º";
}
// IConvertible implementations.
public TypeCode GetTypeCode()
{
return TypeCode.Object;
}
public bool ToBoolean(IFormatProvider provider)
{
throw new InvalidCastException(String.Format("Temperature-to-Boolean conversion is not supported."));
}
public byte ToByte(IFormatProvider provider)
{
if (temp < Byte.MinValue || temp > Byte.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the Byte data type.", temp));
else
return (byte)temp;
}
public char ToChar(IFormatProvider provider)
{
throw new InvalidCastException("Temperature-to-Char conversion is not supported.");
}
public DateTime ToDateTime(IFormatProvider provider)
{
throw new InvalidCastException("Temperature-to-DateTime conversion is not supported.");
}
public decimal ToDecimal(IFormatProvider provider)
{
return temp;
}
public double ToDouble(IFormatProvider provider)
{
return (double)temp;
}
public short ToInt16(IFormatProvider provider)
{
if (temp < Int16.MinValue || temp > Int16.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the Int16 data type.", temp));
else
return (short)Math.Round(temp);
}
public int ToInt32(IFormatProvider provider)
{
if (temp < Int32.MinValue || temp > Int32.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the Int32 data type.", temp));
else
return (int)Math.Round(temp);
}
public long ToInt64(IFormatProvider provider)
{
if (temp < Int64.MinValue || temp > Int64.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the Int64 data type.", temp));
else
return (long)Math.Round(temp);
}
public sbyte ToSByte(IFormatProvider provider)
{
if (temp < SByte.MinValue || temp > SByte.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the SByte data type.", temp));
else
return (sbyte)temp;
}
public float ToSingle(IFormatProvider provider)
{
return (float)temp;
}
public virtual string ToString(IFormatProvider provider)
{
return temp.ToString(provider) + "°";
}
// If conversionType is implemented by another IConvertible method, call it.
public virtual object ToType(Type conversionType, IFormatProvider provider)
{
switch (Type.GetTypeCode(conversionType))
{
case TypeCode.Boolean:
return this.ToBoolean(provider);
case TypeCode.Byte:
return this.ToByte(provider);
case TypeCode.Char:
return this.ToChar(provider);
case TypeCode.DateTime:
return this.ToDateTime(provider);
case TypeCode.Decimal:
return this.ToDecimal(provider);
case TypeCode.Double:
return this.ToDouble(provider);
case TypeCode.Empty:
throw new NullReferenceException("The target type is null.");
case TypeCode.Int16:
return this.ToInt16(provider);
case TypeCode.Int32:
return this.ToInt32(provider);
case TypeCode.Int64:
return this.ToInt64(provider);
case TypeCode.Object:
// Leave conversion of non-base types to derived classes.
throw new InvalidCastException(String.Format("Cannot convert from Temperature to {0}.",
conversionType.Name));
case TypeCode.SByte:
return this.ToSByte(provider);
case TypeCode.Single:
return this.ToSingle(provider);
case TypeCode.String:
IConvertible iconv = this;
return iconv.ToString(provider);
case TypeCode.UInt16:
return this.ToUInt16(provider);
case TypeCode.UInt32:
return this.ToUInt32(provider);
case TypeCode.UInt64:
return this.ToUInt64(provider);
default:
throw new InvalidCastException("Conversion not supported.");
}
}
public ushort ToUInt16(IFormatProvider provider)
{
if (temp < UInt16.MinValue || temp > UInt16.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the UInt16 data type.", temp));
else
return (ushort)Math.Round(temp);
}
public uint ToUInt32(IFormatProvider provider)
{
if (temp < UInt32.MinValue || temp > UInt32.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the UInt32 data type.", temp));
else
return (uint)Math.Round(temp);
}
public ulong ToUInt64(IFormatProvider provider)
{
if (temp < UInt64.MinValue || temp > UInt64.MaxValue)
throw new OverflowException(String.Format("{0} is out of range of the UInt64 data type.", temp));
else
return (ulong)Math.Round(temp);
}
}
public class TemperatureCelsius : Temperature, IConvertible
{
public TemperatureCelsius(decimal value) : base(value)
{
}
// Override ToString methods.
public override string ToString()
{
return this.ToString(null);
}
public override string ToString(IFormatProvider provider)
{
return temp.ToString(provider) + "°C";
}
// If conversionType is a implemented by another IConvertible method, call it.
public override object ToType(Type conversionType, IFormatProvider provider)
{
// For non-objects, call base method.
if (Type.GetTypeCode(conversionType) != TypeCode.Object)
{
return base.ToType(conversionType, provider);
}
else
{
if (conversionType.Equals(typeof(TemperatureCelsius)))
return this;
else if (conversionType.Equals(typeof(TemperatureFahrenheit)))
return new TemperatureFahrenheit((decimal)this.temp * 9 / 5 + 32);
else
throw new InvalidCastException(String.Format("Cannot convert from Temperature to {0}.",
conversionType.Name));
}
}
}
public class TemperatureFahrenheit : Temperature, IConvertible
{
public TemperatureFahrenheit(decimal value) : base(value)
{
}
// Override ToString methods.
public override string ToString()
{
return this.ToString(null);
}
public override string ToString(IFormatProvider provider)
{
return temp.ToString(provider) + "°F";
}
public override object ToType(Type conversionType, IFormatProvider provider)
{
// For non-objects, call base method.
if (Type.GetTypeCode(conversionType) != TypeCode.Object)
{
return base.ToType(conversionType, provider);
}
else
{
// Handle conversion between derived classes.
if (conversionType.Equals(typeof(TemperatureFahrenheit)))
return this;
else if (conversionType.Equals(typeof(TemperatureCelsius)))
return new TemperatureCelsius((decimal)(this.temp - 32) * 5 / 9);
// Unspecified object type: throw an InvalidCastException.
else
throw new InvalidCastException(String.Format("Cannot convert from Temperature to {0}.",
conversionType.Name));
}
}
}
Public MustInherit Class Temperature
Implements IConvertible
Protected temp As Decimal
Public Sub New(temperature As Decimal)
Me.temp = temperature
End Sub
Public Property Value As Decimal
Get
Return Me.temp
End Get
Set
Me.temp = Value
End Set
End Property
Public Overrides Function ToString() As String
Return temp.ToString() & "º"
End Function
' IConvertible implementations.
Public Function GetTypeCode() As TypeCode Implements IConvertible.GetTypeCode
Return TypeCode.Object
End Function
Public Function ToBoolean(provider As IFormatProvider) As Boolean Implements IConvertible.ToBoolean
Throw New InvalidCastException(String.Format("Temperature-to-Boolean conversion is not supported."))
End Function
Public Function ToByte(provider As IFormatProvider) As Byte Implements IConvertible.ToByte
If temp < Byte.MinValue Or temp > Byte.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the Byte data type.", temp))
Else
Return CByte(temp)
End If
End Function
Public Function ToChar(provider As IFormatProvider) As Char Implements IConvertible.ToChar
Throw New InvalidCastException("Temperature-to-Char conversion is not supported.")
End Function
Public Function ToDateTime(provider As IFormatProvider) As DateTime Implements IConvertible.ToDateTime
Throw New InvalidCastException("Temperature-to-DateTime conversion is not supported.")
End Function
Public Function ToDecimal(provider As IFormatProvider) As Decimal Implements IConvertible.ToDecimal
Return temp
End Function
Public Function ToDouble(provider As IFormatProvider) As Double Implements IConvertible.ToDouble
Return CDbl(temp)
End Function
Public Function ToInt16(provider As IFormatProvider) As Int16 Implements IConvertible.ToInt16
If temp < Int16.MinValue Or temp > Int16.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the Int16 data type.", temp))
End If
Return CShort(Math.Round(temp))
End Function
Public Function ToInt32(provider As IFormatProvider) As Int32 Implements IConvertible.ToInt32
If temp < Int32.MinValue Or temp > Int32.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the Int32 data type.", temp))
End If
Return CInt(Math.Round(temp))
End Function
Public Function ToInt64(provider As IFormatProvider) As Int64 Implements IConvertible.ToInt64
If temp < Int64.MinValue Or temp > Int64.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the Int64 data type.", temp))
End If
Return CLng(Math.Round(temp))
End Function
Public Function ToSByte(provider As IFormatProvider) As SByte Implements IConvertible.ToSByte
If temp < SByte.MinValue Or temp > SByte.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the SByte data type.", temp))
Else
Return CSByte(temp)
End If
End Function
Public Function ToSingle(provider As IFormatProvider) As Single Implements IConvertible.ToSingle
Return CSng(temp)
End Function
Public Overridable Overloads Function ToString(provider As IFormatProvider) As String Implements IConvertible.ToString
Return temp.ToString(provider) & " °C"
End Function
' If conversionType is a implemented by another IConvertible method, call it.
Public Overridable Function ToType(conversionType As Type, provider As IFormatProvider) As Object Implements IConvertible.ToType
Select Case Type.GetTypeCode(conversionType)
Case TypeCode.Boolean
Return Me.ToBoolean(provider)
Case TypeCode.Byte
Return Me.ToByte(provider)
Case TypeCode.Char
Return Me.ToChar(provider)
Case TypeCode.DateTime
Return Me.ToDateTime(provider)
Case TypeCode.Decimal
Return Me.ToDecimal(provider)
Case TypeCode.Double
Return Me.ToDouble(provider)
Case TypeCode.Empty
Throw New NullReferenceException("The target type is null.")
Case TypeCode.Int16
Return Me.ToInt16(provider)
Case TypeCode.Int32
Return Me.ToInt32(provider)
Case TypeCode.Int64
Return Me.ToInt64(provider)
Case TypeCode.Object
' Leave conversion of non-base types to derived classes.
Throw New InvalidCastException(String.Format("Cannot convert from Temperature to {0}.", _
conversionType.Name))
Case TypeCode.SByte
Return Me.ToSByte(provider)
Case TypeCode.Single
Return Me.ToSingle(provider)
Case TypeCode.String
Return Me.ToString(provider)
Case TypeCode.UInt16
Return Me.ToUInt16(provider)
Case TypeCode.UInt32
Return Me.ToUInt32(provider)
Case TypeCode.UInt64
Return Me.ToUInt64(provider)
Case Else
Throw New InvalidCastException("Conversion not supported.")
End Select
End Function
Public Function ToUInt16(provider As IFormatProvider) As UInt16 Implements IConvertible.ToUInt16
If temp < UInt16.MinValue Or temp > UInt16.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the UInt16 data type.", temp))
End If
Return CUShort(Math.Round(temp))
End Function
Public Function ToUInt32(provider As IFormatProvider) As UInt32 Implements IConvertible.ToUInt32
If temp < UInt32.MinValue Or temp > UInt32.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the UInt32 data type.", temp))
End If
Return CUInt(Math.Round(temp))
End Function
Public Function ToUInt64(provider As IFormatProvider) As UInt64 Implements IConvertible.ToUInt64
If temp < UInt64.MinValue Or temp > UInt64.MaxValue Then
Throw New OverflowException(String.Format("{0} is out of range of the UInt64 data type.", temp))
End If
Return CULng(Math.Round(temp))
End Function
End Class
Public Class TemperatureCelsius : Inherits Temperature : Implements IConvertible
Public Sub New(value As Decimal)
MyBase.New(value)
End Sub
' Override ToString methods.
Public Overrides Function ToString() As String
Return Me.ToString(Nothing)
End Function
Public Overrides Function ToString(provider As IFormatProvider) As String
Return temp.ToString(provider) + "°C"
End Function
' If conversionType is a implemented by another IConvertible method, call it.
Public Overrides Function ToType(conversionType As Type, provider As IFormatProvider) As Object
' For non-objects, call base method.
If Type.GetTypeCode(conversionType) <> TypeCode.Object Then
Return MyBase.ToType(conversionType, provider)
Else
If conversionType.Equals(GetType(TemperatureCelsius)) Then
Return Me
ElseIf conversionType.Equals(GetType(TemperatureFahrenheit))
Return New TemperatureFahrenheit(CDec(Me.temp * 9 / 5 + 32))
' Unspecified object type: throw an InvalidCastException.
Else
Throw New InvalidCastException(String.Format("Cannot convert from Temperature to {0}.", _
conversionType.Name))
End If
End If
End Function
End Class
Public Class TemperatureFahrenheit : Inherits Temperature : Implements IConvertible
Public Sub New(value As Decimal)
MyBase.New(value)
End Sub
' Override ToString methods.
Public Overrides Function ToString() As String
Return Me.ToString(Nothing)
End Function
Public Overrides Function ToString(provider As IFormatProvider) As String
Return temp.ToString(provider) + "°F"
End Function
Public Overrides Function ToType(conversionType As Type, provider As IFormatProvider) As Object
' For non-objects, call base method.
If Type.GetTypeCode(conversionType) <> TypeCode.Object Then
Return MyBase.ToType(conversionType, provider)
Else
' Handle conversion between derived classes.
If conversionType.Equals(GetType(TemperatureFahrenheit)) Then
Return Me
ElseIf conversionType.Equals(GetType(TemperatureCelsius))
Return New TemperatureCelsius(CDec((MyBase.temp - 32) * 5 / 9))
' Unspecified object type: throw an InvalidCastException.
Else
Throw New InvalidCastException(String.Format("Cannot convert from Temperature to {0}.", _
conversionType.Name))
End If
End If
End Function
End Class
O exemplo a seguir ilustra várias chamadas a essas IConvertible implementações para converter TemperatureCelsius
objetos em TemperatureFahrenheit
objetos e vice-versa.
TemperatureCelsius tempC1 = new TemperatureCelsius(0);
TemperatureFahrenheit tempF1 = (TemperatureFahrenheit)Convert.ChangeType(tempC1, typeof(TemperatureFahrenheit), null);
Console.WriteLine($"{tempC1} equals {tempF1}.");
TemperatureCelsius tempC2 = (TemperatureCelsius)Convert.ChangeType(tempC1, typeof(TemperatureCelsius), null);
Console.WriteLine($"{tempC1} equals {tempC2}.");
TemperatureFahrenheit tempF2 = new TemperatureFahrenheit(212);
TemperatureCelsius tempC3 = (TemperatureCelsius)Convert.ChangeType(tempF2, typeof(TemperatureCelsius), null);
Console.WriteLine($"{tempF2} equals {tempC3}.");
TemperatureFahrenheit tempF3 = (TemperatureFahrenheit)Convert.ChangeType(tempF2, typeof(TemperatureFahrenheit), null);
Console.WriteLine($"{tempF2} equals {tempF3}.");
// The example displays the following output:
// 0°C equals 32°F.
// 0°C equals 0°C.
// 212°F equals 100°C.
// 212°F equals 212°F.
Dim tempC1 As New TemperatureCelsius(0)
Dim tempF1 As TemperatureFahrenheit = CType(Convert.ChangeType(tempC1, GetType(TemperatureFahrenheit), Nothing), TemperatureFahrenheit)
Console.WriteLine("{0} equals {1}.", tempC1, tempF1)
Dim tempC2 As TemperatureCelsius = CType(Convert.ChangeType(tempC1, GetType(TemperatureCelsius), Nothing), TemperatureCelsius)
Console.WriteLine("{0} equals {1}.", tempC1, tempC2)
Dim tempF2 As New TemperatureFahrenheit(212)
Dim tempC3 As TEmperatureCelsius = CType(Convert.ChangeType(tempF2, GEtType(TemperatureCelsius), Nothing), TemperatureCelsius)
Console.WriteLine("{0} equals {1}.", tempF2, tempC3)
Dim tempF3 As TemperatureFahrenheit = CType(Convert.ChangeType(tempF2, GetType(TemperatureFahrenheit), Nothing), TemperatureFahrenheit)
Console.WriteLine("{0} equals {1}.", tempF2, tempF3)
' The example displays the following output:
' 0°C equals 32°F.
' 0°C equals 0°C.
' 212°F equals 100°C.
' 212°F equals 212°F.
A classe TypeConverter
O .NET também permite que você defina um conversor de tipo para um tipo personalizado estendendo a System.ComponentModel.TypeConverter classe e associando o conversor de tipo com o tipo por meio de um System.ComponentModel.TypeConverterAttribute atributo. A tabela a seguir destaca as diferenças entre essa abordagem e a implementação da IConvertible interface para um tipo personalizado.
Observação
O suporte no tempo de design somente poderá ser fornecido para um tipo personalizado se houver um conversor de tipo definido para ele.
Conversão usando TypeConverter | Conversão usando IConvertible |
---|---|
É implementado para um tipo personalizado derivando uma classe separada de TypeConverter. Essa classe derivada é associada ao tipo personalizado aplicando um TypeConverterAttribute atributo. | É implementada por um tipo personalizado para executar a conversão. Um usuário do tipo invoca um método de conversão IConvertible no tipo. |
Pode ser usado tanto na fase de design quanto na fase de execução. | Somente pode ser usada no tempo de execução. |
Usa reflexão; portanto, é mais lento do que a conversão habilitada por IConvertible. | Não usa reflexão. |
Permite conversões de tipo bidirecional do tipo personalizado para outros tipos de dados e de outros tipos de dados para o tipo personalizado. Por exemplo, um TypeConverter definido para MyType permite conversões de MyType para String, e de String para MyType . |
Permite a conversão de um tipo personalizado para outros tipos de dados, mas não de outros tipos de dados para o tipo personalizado. |
Para obter mais informações sobre como usar conversores de tipo para executar conversões, consulte System.ComponentModel.TypeConverter.