Table de conversion de type dans .NETType Conversion Tables in .NET
Une conversion étendue se produit quand une valeur d’un type est convertie en un autre type de taille égale ou supérieure.Widening conversion occurs when a value of one type is converted to another type that is of equal or greater size. Une conversion restrictive se produit quand une valeur d’un type est convertie en une valeur d’un autre type de taille inférieure.A narrowing conversion occurs when a value of one type is converted to a value of another type that is of a smaller size. Les tableaux de cette rubrique illustrent les comportements propres aux deux types de conversion.The tables in this topic illustrate the behaviors exhibited by both types of conversions.
conversions étenduesWidening Conversions
Le tableau suivant décrit les conversions étendues qui peuvent être effectuées sans perte d’informations.The following table describes the widening conversions that can be performed without the loss of information.
TypeType | Peut être converti sans perte de données enCan be converted without data loss to |
---|---|
Byte | UInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double, DecimalUInt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal |
SByte | Int16, Int32, Int64, Single, Double, DecimalInt16, Int32, Int64, Single, Double, Decimal |
Int16 | Int32, Int64, Single, Double, DecimalInt32, Int64, Single, Double, Decimal |
UInt16 | UInt32, Int32, UInt64, Int64, Single, Double, DecimalUInt32, Int32, UInt64, Int64, Single, Double, Decimal |
Char | UInt16, UInt32, Int32, UInt64, Int64, Single, Double, DecimalUInt16, UInt32, Int32, UInt64, Int64, Single, Double, Decimal |
Int32 | Int64, Double, DecimalInt64, Double, Decimal |
UInt32 | Int64, UInt64, Double, DecimalInt64, UInt64, Double, Decimal |
Int64 | Decimal |
UInt64 | Decimal |
Single | Double |
Certaines conversions étendues à Single ou Double peuvent entraîner une perte de précision.Some widening conversions to Single or Double can cause a loss of precision. Le tableau suivant décrit les conversions étendues qui entraînent parfois une perte d’informations.The following table describes the widening conversions that sometimes result in a loss of information.
TypeType | Peut être converti enCan be converted to |
---|---|
Int32 | Single |
UInt32 | Single |
Int64 | Single, DoubleSingle, Double |
UInt64 | Single, DoubleSingle, Double |
Decimal | Single, DoubleSingle, Double |
conversions restrictivesNarrowing Conversions
Une conversion restrictive en Single ou Double peut entraîner une perte d’informations.A narrowing conversion to Single or Double can cause a loss of information. Si le type cible ne peut pas exprimer correctement la grandeur de la source, le type résultant est défini sur la constante PositiveInfinity
ou NegativeInfinity
.If the target type cannot properly express the magnitude of the source, the resulting type is set to the constant PositiveInfinity
or NegativeInfinity
. PositiveInfinity
est obtenu en divisant un nombre positif par zéro et est également retourné quand la valeur d’un Single ou d’un Double dépasse la valeur du champ MaxValue
.PositiveInfinity
results from dividing a positive number by zero and is also returned when the value of a Single or Double exceeds the value of the MaxValue
field. NegativeInfinity
est obtenu en divisant un nombre négatif par zéro et est également retourné quand la valeur d’un Single ou d’un Double descend en dessous de la valeur du champ MinValue
.NegativeInfinity
results from dividing a negative number by zero and is also returned when the value of a Single or Double falls below the value of the MinValue
field. Une conversion d’un Double en Single peut avoir pour résultat PositiveInfinity
ou NegativeInfinity
.A conversion from a Double to a Single might result in PositiveInfinity
or NegativeInfinity
.
Une conversion restrictive peut également entraîner une perte d’informations pour d’autres types de données.A narrowing conversion can also result in a loss of information for other data types. Toutefois, une OverflowExceptionest levée si la valeur d’un type en cours de conversion se situe en dehors de la plage spécifiée par les champs MaxValue
et MinValue
du type cible, et la conversion est vérifiée par le runtime pour garantir que la valeur du type cible ne dépasse pas sa valeur MaxValue
ou MinValue
.However, an OverflowException is thrown if the value of a type that is being converted falls outside of the range specified by the target type's MaxValue
and MinValue
fields, and the conversion is checked by the runtime to ensure that the value of the target type does not exceed its MaxValue
or MinValue
. Les conversions effectuées avec la classe System.Convert sont toujours vérifiées de cette façon.Conversions that are performed with the System.Convert class are always checked in this manner.
Le tableau suivant répertorie les conversions qui lèvent une OverflowException à l’aide de System.Convert ou de toute conversion contrôlée si la valeur du type converti se situe en dehors de la plage définie du type résultant.The following table lists conversions that throw an OverflowException using System.Convert or any checked conversion if the value of the type being converted is outside the defined range of the resulting type.
TypeType | Peut être converti enCan be converted to |
---|---|
Byte | SByte |
SByte | Byte, UInt16, UInt32, UInt64Byte, UInt16, UInt32, UInt64 |
Int16 | Byte, SByte, UInt16Byte, SByte, UInt16 |
UInt16 | Byte, SByte, Int16Byte, SByte, Int16 |
Int32 | Byte, SByte, Int16, UInt16,UInt32Byte, SByte, Int16, UInt16,UInt32 |
UInt32 | Byte, SByte, Int16, UInt16, Int32Byte, SByte, Int16, UInt16, Int32 |
Int64 | Byte, SByte, Int16, UInt16, Int32,UInt32,UInt64Byte, SByte, Int16, UInt16, Int32,UInt32,UInt64 |
UInt64 | Byte, SByte, Int16, UInt16, Int32, UInt32, Int64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64 |
Decimal | Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64 |
Single | Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64 |
Double | Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64 |