Integrierte numerische Konvertierungen (C#-Referenz)Built-in numeric conversions (C# reference)
C# bietet eine Reihe von integralen numerischen Typen und numerischen Gleitkommatypen.C# provides a set of integral and floating-point numeric types. Es gibt eine Konvertierung zwischen zwei beliebigen numerischen Typen, entweder implizit oder explizit.There exists a conversion between any two numeric types, either implicit or explicit. Sie müssen einen Cast-Ausdruck verwenden, um eine explizite Konvertierung durchzuführen.You must use a cast expression to perform an explicit conversion.
Implizite numerische KonvertierungenImplicit numeric conversions
Folgende Tabelle veranschaulicht vordefinierte implizite Konvertierungen zwischen integrierten numerischen Typen:The following table shows the predefined implicit conversions between the built-in numeric types:
VonFrom | BeschreibungTo |
---|---|
sbytesbyte | short , int , long , float , double oder decimal short , int , long , float , double , or decimal |
bytebyte | short , ushort , int , uint , long , ulong , float , double oder decimal short , ushort , int , uint , long , ulong , float , double , or decimal |
shortshort | int , long , float , double oder decimal int , long , float , double , or decimal |
ushortushort | int , uint , long , ulong , float , double oder decimal int , uint , long , ulong , float , double , or decimal |
intint | long , float , double oder decimal long , float , double , or decimal |
uintuint | long , ulong , float , double oder decimal long , ulong , float , double , or decimal |
longlong | float , double oder decimal float , double , or decimal |
ulongulong | float , double oder decimal float , double , or decimal |
floatfloat | double |
Hinweis
Die impliziten Konvertierungen von int
, uint
, long
oder ulong
in float
und von long
oder ulong
in double
können zu einem Verlust an Genauigkeit führen, aber nie zu einem Verlust in einer Größenordnung.The implicit conversions from int
, uint
, long
, or ulong
to float
and from long
or ulong
to double
may cause a loss of precision, but never a loss of an order of magnitude. Die anderen impliziten numerischen Konvertierungen verlieren nie Informationen.The other implicit numeric conversions never lose any information.
Beachten Sie außerdem:Also note that
Jeder integrale numerische Typ ist implizit in jeden numerischen Gleitkommatypen konvertierbar.Any integral numeric type is implicitly convertible to any floating-point numeric type.
Es gibt keine impliziten Konvertierungen in die Typen
byte
undsbyte
.There are no implicit conversions to thebyte
andsbyte
types. Es gibt keine impliziten Konvertierungen aus den Typendouble
unddecimal
.There are no implicit conversions from thedouble
anddecimal
types.Es gibt keine impliziten Konvertierungen zwischen dem Typ
decimal
und dem Typfloat
oderdouble
.There are no implicit conversions between thedecimal
type and thefloat
ordouble
types.Ein Wert eines konstanten Ausdrucks vom Typ
int
(z. B. ein Wert, der durch ein ganzzahliges Literal dargestellt wird), kann implizit insbyte
,byte
,short
,ushort
,uint
oderulong
konvertiert werden, wenn er sich im Bereich des Zieltyps befindet:A value of a constant expression of typeint
(for example, a value represented by an integer literal) can be implicitly converted tosbyte
,byte
,short
,ushort
,uint
, orulong
, if it's within the range of the destination type:byte a = 13; byte b = 300; // CS0031: Constant value '300' cannot be converted to a 'byte'
Wie das vorherige Beispiel zeigt, tritt der Compilerfehler CS0031 auf, wenn der konstante Wert nicht innerhalb des Bereichs des Zieltyps liegt.As the preceding example shows, if the constant value is not within the range of the destination type, a compiler error CS0031 occurs.
Explizite numerische KonvertierungenExplicit numeric conversions
Folgende Tabelle veranschaulicht vordefinierte explizite Konvertierungen zwischen integrierten numerischen Typen, für die es keine implizite Konvertierung gibt:The following table shows the predefined explicit conversions between the built-in numeric types for which there is no implicit conversion:
VonFrom | BeschreibungTo |
---|---|
sbytesbyte | byte , ushort , uint oder ulong byte , ushort , uint , or ulong |
bytebyte | sbyte |
shortshort | sbyte , byte , ushort , uint oder ulong sbyte , byte , ushort , uint , or ulong |
ushortushort | sbyte , byte oder short sbyte , byte , or short |
intint | sbyte , byte , short , ushort , uint oder ulong sbyte , byte , short , ushort , uint , or ulong |
uintuint | sbyte , byte , short , ushort oder int sbyte , byte , short , ushort , or int |
longlong | sbyte , byte , short , ushort , int , uint oder ulong sbyte , byte , short , ushort , int , uint , or ulong |
ulongulong | sbyte , byte , short , ushort , int , uint oder long sbyte , byte , short , ushort , int , uint , or long |
floatfloat | sbyte , byte , short , ushort , int , uint , long , ulong oder decimal sbyte , byte , short , ushort , int , uint , long , ulong , or decimal |
doubledouble | sbyte , byte , short , ushort , int , uint , long , ulong , float oder decimal sbyte , byte , short , ushort , int , uint , long , ulong , float , or decimal |
decimaldecimal | sbyte , byte , short , ushort , int , uint , long , ulong , float oder double sbyte , byte , short , ushort , int , uint , long , ulong , float , or double |
Hinweis
Eine explizite numerische Konvertierung kann zu Datenverlust führen oder eine Ausnahme auslösen, typischerweise ein OverflowException.An explicit numeric conversion might result in data loss or throw an exception, typically an OverflowException.
Beachten Sie außerdem:Also note that
Wenn Sie einen Wert von einem integralen Typ in einen anderen integralen Typ konvertieren, ist das Ergebnis vom Kontext der Überlaufprüfung abhängig.When you convert a value of an integral type to another integral type, the result depends on the overflow checking context. Die Konvertierung in einem geprüften Kontext ist erfolgreich, wenn der Quellwert sich innerhalb des Bereichs des Zieltyps befindet.In a checked context, the conversion succeeds if the source value is within the range of the destination type. Andernfalls wird eine OverflowException ausgelöst.Otherwise, an OverflowException is thrown. In einem ungeprüften Kontext ist die Konvertierung immer erfolgreich, und sie verläuft wie folgt:In an unchecked context, the conversion always succeeds, and proceeds as follows:
Wenn der Quelltyp größer als der Zieltyp ist, wird der Quellwert abgeschnitten, indem die wichtigsten „zusätzlichen“ Teile verworfen werden.If the source type is larger than the destination type, then the source value is truncated by discarding its "extra" most significant bits. Das Ergebnis wird dann als Wert des Zieltyps behandelt.The result is then treated as a value of the destination type.
Wenn der Quelltyp kleiner als der Zieltyp ist, ist der Quellwert entweder signaturerweitert oder mit Null erweitert, sodass er die gleiche Größe wie der Zieltyp hat.If the source type is smaller than the destination type, then the source value is either sign-extended or zero-extended so that it's of the same size as the destination type. Die Vorzeichenerweiterung wird verwendet, wenn der Quelltyp mit einem Vorzeichen versehen ist. Die Erweiterung mit Nullen (0) wird verwendet, wenn der Quelltyp mit keinem Vorzeichen versehen ist.Sign-extension is used if the source type is signed; zero-extension is used if the source type is unsigned. Das Ergebnis wird dann als Wert des Zieltyps behandelt.The result is then treated as a value of the destination type.
Wenn der Quelltyp die gleiche Größe wie der Zieltyp aufweist, wird der Quellwert als Wert vom Zieltyp behandelt.If the source type is the same size as the destination type, then the source value is treated as a value of the destination type.
Wenn Sie einen
decimal
-Wert in einen integralen Typ konvertieren, wird dieser Wert Richtung 0 (null) auf den nächsten Integralwert gerundet.When you convert adecimal
value to an integral type, this value is rounded towards zero to the nearest integral value. Wenn der erzeugte Integralwert sich außerhalb des Bereichs des Zieltyps befindet, wird eine OverflowException ausgelöst.If the resulting integral value is outside the range of the destination type, an OverflowException is thrown.Wenn Sie einen
double
- oderfloat
-Wert in einen integralen Typ konvertieren, wird dieser Wert Richtung 0 (null) auf den nächsten Integralwert gerundet.When you convert adouble
orfloat
value to an integral type, this value is rounded towards zero to the nearest integral value. Wenn der resultierende Integralwert sich außerhalb des Bereichs des Zieltyps befindet, hängt das Ergebnis vom Kontext der Überlaufprüfung ab.If the resulting integral value is outside the range of the destination type, the result depends on the overflow checking context. In einem geprüften Kontext wird eine OverflowException ausgelöst, während das Ergebnis in einem ungeprüften Kontext ein nicht angegebener Wert des Zieltyps ist.In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.Wenn Sie
double
infloat
konvertieren, wird derdouble
-Wert auf den nächstenfloat
-Wert gerundet.When you convertdouble
tofloat
, thedouble
value is rounded to the nearestfloat
value. Wenn derdouble
-Wert zu klein oder zu groß ist, um in denfloat
-Typ zu passen, ist das Ergebnis 0 (null) oder unendlich.If thedouble
value is too small or too large to fit into thefloat
type, the result is zero or infinity.Wenn Sie
float
oderdouble
indecimal
konvertieren, wird der Quellwert in einedecimal
-Darstellung konvertiert und bei Bedarf auf die nächste Zahl nach der achtundzwanzigsten Dezimalstelle gerundet.When you convertfloat
ordouble
todecimal
, the source value is converted todecimal
representation and rounded to the nearest number after the 28th decimal place if required. Je nach Wert des Quellwerts kann eines der folgenden Ergebnisse auftreten:Depending on the value of the source value, one of the following results may occur:Wenn der Quellwert zu klein ist, als dass er als
decimal
dargestellt werden könnte, ist das Ergebnis 0 (null).If the source value is too small to be represented as adecimal
, the result becomes zero.Wenn der Quellwert NaN (nicht numerisch), unendlich oder zu groß ist, um als
decimal
dargestellt zu werden, wird eine OverflowException ausgelöst.If the source value is NaN (not a number), infinity, or too large to be represented as adecimal
, an OverflowException is thrown.
Wenn Sie
decimal
infloat
oderdouble
konvertieren, wird der Quellwert in den nächstenfloat
- oderdouble
-Wert konvertiert.When you convertdecimal
tofloat
ordouble
, the source value is rounded to the nearestfloat
ordouble
value, respectively.
C#-SprachspezifikationC# language specification
Weitere Informationen finden Sie in den folgenden Abschnitten der C#-Sprachspezifikation:For more information, see the following sections of the C# language specification:
- Implizite numerische KonvertierungenImplicit numeric conversions
- Explizite numerische KonvertierungenExplicit numeric conversions