BigInteger.Explicit 运算符

定义

定义 BigInteger 对象与其他类型之间的显式转换。Defines an explicit conversion between a BigInteger object and another type.

重载

Explicit(Decimal to BigInteger)

定义从 Decimal 对象到 BigInteger 值的显式转换。Defines an explicit conversion of a Decimal object to a BigInteger value.

Explicit(Double to BigInteger)

定义从 Double 值到 BigInteger 值的显式转换。Defines an explicit conversion of a Double value to a BigInteger value.

Explicit(BigInteger to Byte)

定义从 BigInteger 对象到无符号字节值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned byte value.

Explicit(BigInteger to Decimal)

定义从 BigInteger 对象到 Decimal 值的显式转换。Defines an explicit conversion of a BigInteger object to a Decimal value.

Explicit(BigInteger to Double)

定义从 BigInteger 对象到 Double 值的显式转换。Defines an explicit conversion of a BigInteger object to a Double value.

Explicit(BigInteger to Int16)

定义从 BigInteger 对象到 16 位带符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to a 16-bit signed integer value.

Explicit(BigInteger to Int32)

定义从 BigInteger 对象到 32 位带符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to a 32-bit signed integer value.

Explicit(BigInteger to Int64)

定义从 BigInteger 对象到 64 位带符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to a 64-bit signed integer value.

Explicit(BigInteger to SByte)

定义从 BigInteger 对象到 8 位带符号值的显式转换。Defines an explicit conversion of a BigInteger object to a signed 8-bit value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 Int16The compliant alternative is Int16.

Explicit(BigInteger to Single)

定义从 BigInteger 对象到单精度浮点值的显式转换。Defines an explicit conversion of a BigInteger object to a single-precision floating-point value.

Explicit(BigInteger to UInt16)

定义从 BigInteger 对象到 16 位无符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned 16-bit integer value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 Int32The compliant alternative is Int32.

Explicit(BigInteger to UInt32)

定义从 BigInteger 对象到 32 位无符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned 32-bit integer value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 Int64The compliant alternative is Int64.

Explicit(BigInteger to UInt64)

定义从 BigInteger 对象到 64 位无符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned 64-bit integer value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 DoubleThe compliant alternative is Double.

Explicit(Single to BigInteger)

定义从 Single 值到 BigInteger 值的显式转换。Defines an explicit conversion of a Single value to a BigInteger value.

Explicit(Decimal to BigInteger)

定义从 Decimal 对象到 BigInteger 值的显式转换。Defines an explicit conversion of a Decimal object to a BigInteger value.

public:
 static explicit operator System::Numerics::BigInteger(System::Decimal value);
public static explicit operator System.Numerics.BigInteger (decimal value);
static member op_Explicit : decimal -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Decimal) As BigInteger

参数

value
Decimal

要转换为 BigInteger 的值。The value to convert to a BigInteger.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

示例

下面的示例将 Decimal 值数组中的单个元素转换为 BigInteger 对象,然后显示每个转换的结果。The following example converts the individual elements in an array of Decimal values to BigInteger objects, and then displays the result of each conversion. 请注意,在转换过程中,Decimal 值的任何小数部分将被截断。Note that any fractional part of a Decimal value is truncated during the conversion.

decimal[] decimals = { Decimal.MinValue, -15632.991m, 9029321.12m, 
                       Decimal.MaxValue };
BigInteger number; 

Console.WriteLine("{0,35} {1,35}\n", "Decimal", "BigInteger");

foreach (decimal value in decimals)
{
   number = (BigInteger) value;
   Console.WriteLine("{0,35} {1,35}", value, number);
}
// The example displays the following output:
//
//                          Decimal                          BigInteger
//    
//    -79228162514264337593543950335      -79228162514264337593543950335
//                       -15632.991                              -15632
//                       9029321.12                             9029321
//    79228162514264337593543950335       79228162514264337593543950335
' Explicit Decimal to BigInteger conversion
Dim decimals() As Decimal = { Decimal.MinValue, -15632.991d, 9029321.12d, 
                              Decimal.MaxValue }
Dim number As BigInteger 

Console.WriteLine("{0,35} {1,35}", "Decimal", "BigInteger")
Console.WriteLine()
For Each value As Decimal In decimals
   number = CType(value, BigInteger)
   Console.WriteLine("{0,35} {1,35}",
                     value, number)
Next
' The example displays the following output:
'
'                          Decimal                          BigInteger
'    
'    -79228162514264337593543950335      -79228162514264337593543950335
'                       -15632.991                              -15632
'                       9029321.12                             9029321
'    79228162514264337593543950335       79228162514264337593543950335

注解

在转换前截断 value 参数的任何小数部分。Any fractional part of the value parameter is truncated before conversion.

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 由于从 DecimalBigInteger 的转换可能涉及截断 value的任何小数部分,因此语言编译器不会自动执行此转换。Because the conversion from Decimal to BigInteger can involve truncating any fractional part of value, language compilers do not perform this conversion automatically. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 Visual Basic 中的 CType)时,它们才会执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

对于不支持自定义运算符的语言,将 BigInteger.BigInteger(Decimal)替代方法。For languages that do not support custom operators, the alternative method is BigInteger.BigInteger(Decimal).

Explicit(Double to BigInteger)

定义从 Double 值到 BigInteger 值的显式转换。Defines an explicit conversion of a Double value to a BigInteger value.

public:
 static explicit operator System::Numerics::BigInteger(double value);
public static explicit operator System.Numerics.BigInteger (double value);
static member op_Explicit : double -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Double) As BigInteger

参数

value
Double

要转换为 BigInteger 的值。The value to convert to a BigInteger.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

示例

下面的示例将 Double 值数组中的单个元素转换为 BigInteger 对象,然后显示每个转换的结果。The following example converts the individual elements in an array of Double values to BigInteger objects, and then displays the result of each conversion. 请注意,在转换过程中,Double 值的任何小数部分将被截断。Note that any fractional part of a Double value is truncated during the conversion.

double[] doubles = { Double.MinValue, -1.430955172e03, 2.410970032e05, 
                     Double.MaxValue, Double.PositiveInfinity, 
                     Double.NegativeInfinity, Double.NaN };
BigInteger number;

Console.WriteLine("{0,37} {1,37}\n", "Double", "BigInteger");

foreach (double value in doubles)
{
   try {
      number = (BigInteger) value;
      Console.WriteLine("{0,37} {1,37}", value, number);
   }   
   catch (OverflowException) {
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
   }
}
// The example displays the following output:
//                                Double                            BigInteger
// 
//                -1.79769313486232E+308  -1.7976931348623157081452742373E+308
//                          -1430.955172                                 -1430
//                           241097.0032                                241097
//                 1.79769313486232E+308   1.7976931348623157081452742373E+308
//                              Infinity                     OverflowException
//                             -Infinity                     OverflowException
//                                   NaN                     OverflowException      
Dim doubles() As Double = { Double.MinValue, -1.430955172e03, 2.410970032e05, 
                            Double.MaxValue, Double.PositiveInfinity, 
                            Double.NegativeInfinity, Double.NaN }
Dim number As BigInteger

Console.WriteLine("{0,37} {1,37}", "Double", "BigInteger")
Console.WriteLine()
For Each value As Double In doubles
   Try
      number = CType(value, BigInteger)
      Console.WriteLine("{0,37} {1,37}", value, number)
   Catch e As OverflowException
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException")
   End Try      
Next
' The example displays the following output:
'                                Double                            BigInteger
' 
'                -1.79769313486232E+308  -1.7976931348623157081452742373E+308
'                          -1430.955172                                 -1430
'                           241097.0032                                241097
'                 1.79769313486232E+308   1.7976931348623157081452742373E+308
'                              Infinity                     OverflowException
'                             -Infinity                     OverflowException
'                                   NaN                     OverflowException      

注解

在转换前截断 value 参数的任何小数部分。Any fractional part of the value parameter is truncated before conversion.

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 由于从 DoubleBigInteger 的转换可能涉及截断 value的任何小数部分,因此语言编译器不会自动执行此转换。Because the conversion from Double to BigInteger can involve truncating any fractional part of value, language compilers do not perform this conversion automatically. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 Visual Basic 中的 CType)时,它们才会执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

对于不支持自定义运算符的语言,将 BigInteger.BigInteger(Double)替代方法。For languages that do not support custom operators, the alternative method is BigInteger.BigInteger(Double).

Explicit(BigInteger to Byte)

定义从 BigInteger 对象到无符号字节值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned byte value.

public:
 static explicit operator System::Byte(System::Numerics::BigInteger value);
public static explicit operator byte (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> byte
Public Shared Narrowing Operator CType (value As BigInteger) As Byte

参数

value
BigInteger

要转换为 Byte 的值。The value to convert to a Byte.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or greater than MaxValue.

示例

下面的示例说明 BigIntegerByte 值的转换。The following example illustrates the conversion of BigInteger to Byte values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 Byte 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Byte data type.

// BigInteger to Byte conversion.
BigInteger goodByte = BigInteger.One;
BigInteger badByte = 256;

byte byteFromBigInteger;   

// Successful conversion using cast operator.
byteFromBigInteger = (byte) goodByte;
Console.WriteLine(byteFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   byteFromBigInteger = (byte) badByte;
   Console.WriteLine(byteFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badByte, e.Message);
}
Console.WriteLine();
' BigInteger to Byte conversion.
Dim goodByte As BigInteger = BigInteger.One
Dim badByte As BigInteger = 256

Dim byteFromBigInteger As Byte   

' Convert using CType function.
byteFromBigInteger = CType(goodByte, Byte)
Console.WriteLine(byteFromBigInteger)
' Convert using CByte function.
byteFromBigInteger = CByte(goodByte)
Console.WriteLine(byteFromBigInteger)

' Handle conversion that should result in overflow.
Try
   byteFromBigInteger = CType(badByte, Byte)
   Console.WriteLine(byteFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badByte, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCByte 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CByte in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 Byte 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Byte data type. 如果转换成功,则结果 Byte 值中不会有精度损失。There is no loss of precision in the resulting Byte value if the conversion is successful.

Explicit(BigInteger to Decimal)

定义从 BigInteger 对象到 Decimal 值的显式转换。Defines an explicit conversion of a BigInteger object to a Decimal value.

public:
 static explicit operator System::Decimal(System::Numerics::BigInteger value);
public static explicit operator decimal (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> decimal
Public Shared Narrowing Operator CType (value As BigInteger) As Decimal

参数

value
BigInteger

要转换为 Decimal 的值。The value to convert to a Decimal.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or greater than MaxValue.

示例

下面的示例说明 BigIntegerDecimal 值的转换。The following example illustrates the conversion of BigInteger to Decimal values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 Decimal 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Decimal data type.

// BigInteger to Decimal conversion.
BigInteger goodDecimal = 761652543;
BigInteger badDecimal = (BigInteger) Decimal.MaxValue; 
badDecimal += BigInteger.One;

Decimal decimalFromBigInteger;

// Successful conversion using cast operator.
decimalFromBigInteger = (decimal) goodDecimal;
Console.WriteLine(decimalFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   decimalFromBigInteger = (decimal) badDecimal;
   Console.WriteLine(decimalFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badDecimal, e.Message);
}
Console.WriteLine();
' BigInteger to Decimal conversion.
Dim goodDecimal As BigInteger = 761652543
Dim badDecimal As BigInteger = CType(Decimal.MaxValue, BigInteger) 
badDecimal += BigInteger.One

Dim decimalFromBigInteger As Decimal

' Convert using CType function.
decimalFromBigInteger = CType(goodDecimal, Decimal)
Console.WriteLine(decimalFromBigInteger)
' Convert using CDec function.
decimalFromBigInteger = CDec(goodDecimal)
Console.WriteLine(decimalFromBigInteger)

' Handle conversion that should result in overflow.
Try
   decimalFromBigInteger = CType(badDecimal, Decimal)
   Console.WriteLine(decimalFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badDecimal, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCDec 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CDec in Visual Basic) is used.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 Decimal 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Decimal data type.

Explicit(BigInteger to Double)

定义从 BigInteger 对象到 Double 值的显式转换。Defines an explicit conversion of a BigInteger object to a Double value.

public:
 static explicit operator double(System::Numerics::BigInteger value);
public static explicit operator double (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> double
Public Shared Narrowing Operator CType (value As BigInteger) As Double

参数

value
BigInteger

要转换为 Double 的值。The value to convert to a Double.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

示例

下面的示例说明 BigIntegerDouble 值的转换。The following example illustrates the conversion of BigInteger to Double values.

// BigInteger to Double conversion.
BigInteger goodDouble = (BigInteger) 102.43e22;
BigInteger badDouble = (BigInteger) Double.MaxValue;  
badDouble = badDouble * 2;

double doubleFromBigInteger;

// successful conversion using cast operator.
doubleFromBigInteger = (double) goodDouble;
Console.WriteLine(doubleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Double.
doubleFromBigInteger = (double) badDouble;
Console.WriteLine(doubleFromBigInteger);
' BigInteger to Double conversion.
Dim goodDouble As BigInteger = 102.43e22
Dim badDouble As BigInteger = CType(Double.MaxValue, BigInteger)  
badDouble = badDouble * 2

Dim doubleFromBigInteger As Double

' Convert using CType function.
doubleFromBigInteger = CType(goodDouble, Double)
Console.WriteLine(doubleFromBigInteger)
' Convert using CDbl function.
doubleFromBigInteger = CDbl(goodDouble)
Console.WriteLine(doubleFromBigInteger)

' Convert an out-of-bounds BigInteger value to a Double.
doubleFromBigInteger = CType(badDouble, Double)
Console.WriteLine(doubleFromBigInteger)

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCDbl 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CDbl in Visual Basic) is used.

由于 BigInteger 值可以超出 Double 数据类型的范围,因此此操作是收缩转换。Because the BigInteger value can be outside the range of the Double data type, this operation is a narrowing conversion. 如果转换失败,则不会引发 OverflowExceptionIf the conversion is unsuccessful, it does not throw an OverflowException. 相反,如果 BigInteger 值小于 Double.MinValue,则生成的 DoubleDouble.NegativeInfinityInstead, if the BigInteger value is less than Double.MinValue, the resulting Double value is Double.NegativeInfinity. 如果 BigInteger 值大于 Double.MaxValue,则生成的 Double 值为 Double.PositiveInfinityIf the BigInteger value is greater than Double.MaxValue, the resulting Double value is Double.PositiveInfinity.

BigIntegerDouble 的转换可能会导致精度损失。The conversion of a BigInteger to a Double may involve a loss of precision. 在某些情况下,丢失精度可能导致强制转换或转换操作成功,即使 BigInteger 值超出 Double 数据类型的范围。In some cases, the loss of precision may cause the casting or conversion operation to succeed even if the BigInteger value is outside the range of the Double data type. 下例对此进行了说明。The following example provides an illustration. 它将 Double 的最大值分配给两个 BigInteger 变量,通过 9.999 e291 增加一个 BigInteger 变量,并测试这两个变量是否相等。It assigns the maximum value of a Double to two BigInteger variables, increments one BigInteger variable by 9.999e291, and tests the two variables for equality. 与预期一样,对 BigInteger.Equals(BigInteger) 方法的调用显示它们不相等。As expected, the call to the BigInteger.Equals(BigInteger) method shows that they are unequal. 但是,虽然 BigInteger 值现在超出 Double.MaxValue,但将较大的 BigInteger 值转换回 Double 会成功。However, the conversion of the larger BigInteger value back to a Double succeeds, although the BigInteger value now exceeds Double.MaxValue.

// Increase a BigInteger so it exceeds Double.MaxValue.
BigInteger number1 = (BigInteger) Double.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e291;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));

// Convert the BigInteger to a Double.
double dbl = (double) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Double:     {0}", dbl); 
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 1.7976931348623158081352742373E+308
//       Double:     1.79769313486232E+308      
' Increase a BigInteger so it exceeds Double.MaxValue.
Dim number1 As BigInteger = CType(Double.MaxValue, BigInteger)
Dim number2 As BigInteger = number1
number2 = number2 + 9.999e291
' Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1))

' Convert the BigInteger to a Double.
Dim dbl As Double = CType(number2, Double)

' Display the two values.
Console.WriteLine("BigInteger: {0}", number2)
Console.WriteLine("Double:     {0}", dbl)      
' The example displays the following output:
'       BigIntegers equal: False
'       BigInteger: 1.7976931348623158081352742373E+308
'       Double:     1.79769313486232E+308      

Explicit(BigInteger to Int16)

定义从 BigInteger 对象到 16 位带符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to a 16-bit signed integer value.

public:
 static explicit operator short(System::Numerics::BigInteger value);
public static explicit operator short (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> int16
Public Shared Narrowing Operator CType (value As BigInteger) As Short

参数

value
BigInteger

要转换为 16 位带符号整数的值。The value to convert to a 16-bit signed integer.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerInt16 值的转换。The following example illustrates the conversion of BigInteger to Int16 values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 Int16 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Int16 data type.

// BigInteger to Int16 conversion.
BigInteger goodShort = 20000;
BigInteger badShort = 33000;

short shortFromBigInteger;

// Successful conversion using cast operator. 
shortFromBigInteger = (short) goodShort;
Console.WriteLine(shortFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   shortFromBigInteger = (short) badShort;
   Console.WriteLine(shortFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badShort, e.Message);
}
Console.WriteLine();
' BigInteger to Int16 conversion.
Dim goodShort As BigInteger = 20000
Dim badShort As BigInteger = 33000

Dim shortFromBigInteger As Short

' Convert using CType function.
shortFromBigInteger = CType(goodShort, Short)
Console.WriteLine(shortFromBigInteger)
' Convert using CShort function.
shortFromBigInteger = CShort(goodShort)
Console.WriteLine(shortFromBigInteger)

' Handle conversion that should result in overflow.
Try
   shortFromBigInteger = CType(badShort, Short)
   Console.WriteLine(shortFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badShort, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCShort 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CShort in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 Int16 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Int16 data type. 如果转换成功,则结果 Int16 值中不会有精度损失。There is no loss of precision in the resulting Int16 value if the conversion is successful.

Explicit(BigInteger to Int32)

定义从 BigInteger 对象到 32 位带符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to a 32-bit signed integer value.

public:
 static explicit operator int(System::Numerics::BigInteger value);
public static explicit operator int (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> int
Public Shared Narrowing Operator CType (value As BigInteger) As Integer

参数

value
BigInteger

要转换为 32 位带符号整数的值。The value to convert to a 32-bit signed integer.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerInt32 值的转换。The following example illustrates the conversion of BigInteger to Int32 values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 Int32 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Int32 data type.

// BigInteger to Int32 conversion.
BigInteger goodInteger = 200000;
BigInteger badInteger = 65000000000;

int integerFromBigInteger;

// Successful conversion using cast operator. 
integerFromBigInteger = (int) goodInteger;
Console.WriteLine(integerFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   integerFromBigInteger = (int) badInteger;
   Console.WriteLine(integerFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badInteger, e.Message);
}
Console.WriteLine();
' BigInteger to Int32 conversion.
Dim goodInteger As BigInteger = 200000
Dim badInteger As BigInteger = 65000000000

Dim integerFromBigInteger As Integer

' Convert using CType function.
integerFromBigInteger = CType(goodInteger, Integer)
Console.WriteLine(integerFromBigInteger)
' Convert using CInt function.
integerFromBigInteger = CInt(goodInteger)
Console.WriteLIne(integerFromBigInteger)

' Handle conversion that should result in overflow.
Try
   integerFromBigInteger = CType(badInteger, Integer)
   Console.WriteLine(integerFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badInteger, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCInt 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CInt in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 Int32 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Int32 data type. 如果转换成功,则结果 Int16 值中不会有精度损失。There is no loss of precision in the resulting Int16 value if the conversion is successful.

Explicit(BigInteger to Int64)

定义从 BigInteger 对象到 64 位带符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to a 64-bit signed integer value.

public:
 static explicit operator long(System::Numerics::BigInteger value);
public static explicit operator long (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> int64
Public Shared Narrowing Operator CType (value As BigInteger) As Long

参数

value
BigInteger

要转换为 64 位带符号整数的值。The value to convert to a 64-bit signed integer.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerInt64 值的转换。The following example illustrates the conversion of BigInteger to Int64 values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 Int64 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the Int64 data type.

// BigInteger to Int64 conversion.
BigInteger goodLong = 2000000000;
BigInteger badLong = BigInteger.Pow(goodLong, 3);

long longFromBigInteger;

// Successful conversion using cast operator. 
longFromBigInteger = (long) goodLong;
Console.WriteLine(longFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   longFromBigInteger = (long) badLong;
   Console.WriteLine(longFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badLong, e.Message);
}
Console.WriteLine();
' BigInteger to Int64 conversion.
Dim goodLong As BigInteger = 2000000000
Dim badLong As BigInteger = BigInteger.Pow(goodLong, 3)

Dim longFromBigInteger As Long

' Convert using CType function.
longFromBigInteger = CType(goodLong, Long)
Console.WriteLine(longFromBigInteger)
' Convert using CLng function.
longFromBigInteger = CLng(goodLong)
Console.WriteLine(longFromBigInteger)

' Handle conversion that should result in overflow.
Try
   longFromBigInteger = CType(badLong, Long)
   Console.WriteLine(longFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badLong, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCLng 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CLng in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 Int64 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the Int64 data type.

Explicit(BigInteger to SByte)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int16

定义从 BigInteger 对象到 8 位带符号值的显式转换。Defines an explicit conversion of a BigInteger object to a signed 8-bit value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 Int16The compliant alternative is Int16.

public:
 static explicit operator System::SByte(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator sbyte (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> sbyte
Public Shared Narrowing Operator CType (value As BigInteger) As SByte

参数

value
BigInteger

要转换为 8 位带符号值的值。The value to convert to a signed 8-bit value.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

属性

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerSByte 值的转换。The following example illustrates the conversion of BigInteger to SByte values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 SByte 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the SByte data type.

// BigInteger to SByte conversion.
BigInteger goodSByte = BigInteger.MinusOne;
BigInteger badSByte = -130;

sbyte sByteFromBigInteger;

// Successful conversion using cast operator. 
sByteFromBigInteger = (sbyte) goodSByte;
Console.WriteLine(sByteFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   sByteFromBigInteger = (sbyte) badSByte;
   Console.WriteLine(sByteFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badSByte, e.Message);
}
Console.WriteLine();
' BigInteger to SByte conversion.
Dim goodSByte As BigInteger = BigInteger.MinusOne
Dim badSByte As BigInteger = -130

Dim sByteFromBigInteger As SByte

' Convert using CType function.
sByteFromBigInteger = CType(goodSByte, SByte)
Console.WriteLine(sByteFromBigInteger)
' Convert using CSByte function.
sByteFromBigInteger = CSByte(goodSByte)
Console.WriteLine(sByteFromBigInteger)

' Handle conversion that should result in overflow.
Try
   sByteFromBigInteger = CType(badSByte, SByte)
   Console.WriteLine(sByteFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badSByte, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCSByte 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CSByte in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 SByte 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the SByte data type. 如果转换成功,则结果 SByte 值中不会有精度损失。There is no loss of precision in the resulting SByte value if the conversion is successful.

Explicit(BigInteger to Single)

定义从 BigInteger 对象到单精度浮点值的显式转换。Defines an explicit conversion of a BigInteger object to a single-precision floating-point value.

public:
 static explicit operator float(System::Numerics::BigInteger value);
public static explicit operator float (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> single
Public Shared Narrowing Operator CType (value As BigInteger) As Single

参数

value
BigInteger

要转换为单精度浮点值的值。The value to convert to a single-precision floating-point value.

返回

一个对象,包含 value 参数的尽可能精确的表示形式。An object that contains the closest possible representation of the value parameter.

示例

下面的示例说明 BigIntegerSingle 值的转换。The following example illustrates the conversion of BigInteger to Single values.

// BigInteger to Single conversion.
BigInteger goodSingle = (BigInteger) 102.43e22F;
BigInteger badSingle = (BigInteger) float.MaxValue;  
badSingle = badSingle * 2;

float singleFromBigInteger;

// Successful conversion using cast operator. 
singleFromBigInteger = (float) goodSingle;
Console.WriteLine(singleFromBigInteger);

// Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = (float) badSingle;
Console.WriteLine(singleFromBigInteger);
' BigInteger to Single conversion.
Dim goodSingle As BigInteger = 102.43e22
Dim badSingle As BigInteger = CType(Single.MaxValue, BigInteger)  
badSingle = badSingle * 2

Dim singleFromBigInteger As Single

' Convert using CType function.
singleFromBigInteger = CType(goodSingle, Single)
Console.WriteLine(singleFromBigInteger)
' Convert using CSng function.
singleFromBigInteger = CSng(goodSingle)
Console.WriteLine(singleFromBigInteger)

' Convert an out-of-bounds BigInteger value to a Single.
singleFromBigInteger = CType(badSingle, Single)
Console.WriteLine(singleFromBigInteger)

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为它可能涉及到数据丢失或精度损失。Language compilers do not perform this conversion automatically because it can involve data loss or a loss of precision. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCSng 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CSng in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于 BigInteger 值可以超出 Single 数据类型的范围,因此此操作是收缩转换。Because the BigInteger value can be outside the range of the Single data type, this operation is a narrowing conversion. 如果转换失败,则不会引发 OverflowExceptionIf the conversion is unsuccessful, it does not throw an OverflowException. 相反,如果 BigInteger 值小于 Single.MinValue,则生成的 SingleSingle.NegativeInfinityInstead, if the BigInteger value is less than Single.MinValue, the resulting Single value is Single.NegativeInfinity. 如果 BigInteger 值大于 Single.MaxValue,则生成的 Single 值为 Single.PositiveInfinityIf the BigInteger value is greater than Single.MaxValue, the resulting Single value is Single.PositiveInfinity.

BigIntegerSingle 的转换可能会导致精度损失。The conversion of a BigInteger to a Single may involve a loss of precision. 在某些情况下,丢失精度可能导致强制转换或转换操作成功,即使 BigInteger 值超出 Single 数据类型的范围。In some cases, the loss of precision may cause the casting or conversion operation to succeed even if the BigInteger value is outside the range of the Single data type. 下例对此进行了说明。The following example provides an illustration. 它将 Single 的最大值分配给两个 BigInteger 变量,通过 9.999 e291 增加一个 BigInteger 变量,并测试这两个变量是否相等。It assigns the maximum value of a Single to two BigInteger variables, increments one BigInteger variable by 9.999e291, and tests the two variables for equality. 与预期一样,对 BigInteger.Equals(BigInteger) 方法的调用显示它们不相等。As expected, the call to the BigInteger.Equals(BigInteger) method shows that they are unequal. 但是,虽然 BigInteger 值现在超出 Single.MaxValue,但将较大的 BigInteger 值转换回 Single 会成功。However, the conversion of the larger BigInteger value back to a Single succeeds, although the BigInteger value now exceeds Single.MaxValue.

// Increase a BigInteger so it exceeds Single.MaxValue.
BigInteger number1 = (BigInteger) Single.MaxValue;
BigInteger number2 = number1;
number2 = number2 + (BigInteger) 9.999e30;
// Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1));

// Convert the BigInteger to a Single.
float sng = (float) number2;

// Display the two values.
Console.WriteLine("BigInteger: {0}", number2);
Console.WriteLine("Single:     {0}", sng); 
// The example displays the following output:
//       BigIntegers equal: False
//       BigInteger: 3.4028235663752885981170396038E+38
//       Single:     3.402823E+38
' Increase a BigInteger so it exceeds Single.MaxValue.
Dim number1 As BigInteger = CType(Single.MaxValue, BigInteger)
Dim number2 As BigInteger = number1
number2 = number2 + 9.999e30
' Compare the BigInteger values for equality.
Console.WriteLine("BigIntegers equal: {0}", number2.Equals(number1))

' Convert the BigInteger to a Single.
Dim sng As Single = CType(number2, Single)

' Display the two values.
Console.WriteLine("BigInteger: {0}", number2)
Console.WriteLine("Single:     {0}", sng)      
' The example displays the following output:
'       BigIntegers equal: False
'       BigInteger: 3.4028235663752885981170396038E+38
'       Single:     3.402823E+38

Explicit(BigInteger to UInt16)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int32

定义从 BigInteger 对象到 16 位无符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned 16-bit integer value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 Int32The compliant alternative is Int32.

public:
 static explicit operator System::UInt16(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator ushort (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> uint16
Public Shared Narrowing Operator CType (value As BigInteger) As UShort

参数

value
BigInteger

要转换为 16 位无符号整数的值。The value to convert to an unsigned 16-bit integer.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

属性

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerUInt16 值的转换。The following example illustrates the conversion of BigInteger to UInt16 values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 UInt16 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the UInt16 data type.

// BigInteger to UInt16 conversion.
BigInteger goodUShort = 20000;
BigInteger badUShort = 66000;

ushort uShortFromBigInteger;

// Successful conversion using cast operator. 
uShortFromBigInteger = (ushort) goodUShort;
Console.WriteLine(uShortFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uShortFromBigInteger = (ushort) badUShort;
   Console.WriteLine(uShortFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badUShort, e.Message);
}
Console.WriteLine();
' BigInteger to UInt16 conversion.
Dim goodUShort As BigInteger = 20000
Dim badUShort As BigInteger = 66000

Dim uShortFromBigInteger As UShort

' Convert using CType function.
uShortFromBigInteger = CType(goodUShort, UShort)
Console.WriteLine(uShortFromBigInteger)
' Convert using CUShort function.
uShortFromBigInteger = CUShort(goodUShort)
Console.WriteLine(uShortFromBigInteger)

' Handle conversion that should result in overflow.
Try
   uShortFromBigInteger = CType(badUShort, UShort)
   Console.WriteLine(uShortFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badUShort, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCUShort 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CUShort in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 UInt16 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the UInt16 data type. 如果转换成功,则结果 UInt16 值中不会有精度损失。There is no loss of precision in the resulting UInt16 value if the conversion is successful.

Explicit(BigInteger to UInt32)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int64

定义从 BigInteger 对象到 32 位无符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned 32-bit integer value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 Int64The compliant alternative is Int64.

public:
 static explicit operator System::UInt32(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator uint (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> uint32
Public Shared Narrowing Operator CType (value As BigInteger) As UInteger

参数

value
BigInteger

要转换为 32 位无符号整数的值。The value to convert to an unsigned 32-bit integer.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

属性

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerUInt32 值的转换。The following example illustrates the conversion of BigInteger to UInt32 values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 UInt32 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the UInt32 data type.

// BigInteger to UInt32 conversion.
BigInteger goodUInteger = 200000;
BigInteger badUInteger = 65000000000;

uint uIntegerFromBigInteger;

// Successful conversion using cast operator. 
uIntegerFromBigInteger = (uint) goodInteger;
Console.WriteLine(uIntegerFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uIntegerFromBigInteger = (uint) badUInteger;
   Console.WriteLine(uIntegerFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badUInteger, e.Message);
}
Console.WriteLine();
' BigInteger to UInt32 conversion.
Dim goodUInteger As BigInteger = 200000
Dim badUInteger As BigInteger = 65000000000

Dim uIntegerFromBigInteger As UInteger

' Convert using CType function.
uIntegerFromBigInteger = CType(goodInteger, UInteger)
Console.WriteLine(uIntegerFromBigInteger)
' Convert using CUInt function.
uIntegerFromBigInteger = CUInt(goodInteger)
Console.WriteLine(uIntegerFromBigInteger)

' Handle conversion that should result in overflow.
Try
   uIntegerFromBigInteger = CType(badUInteger, UInteger)
   Console.WriteLine(uIntegerFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badUInteger, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCUInt 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CUInt in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 UInt32 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the UInt32 data type. 如果转换成功,则结果 UInt32 值中不会有精度损失。There is no loss of precision in the resulting UInt32 value if the conversion is successful.

Explicit(BigInteger to UInt64)

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Double

定义从 BigInteger 对象到 64 位无符号整数值的显式转换。Defines an explicit conversion of a BigInteger object to an unsigned 64-bit integer value.

此 API 不兼容 CLS。This API is not CLS-compliant. 符合的替代方法是 DoubleThe compliant alternative is Double.

public:
 static explicit operator System::UInt64(System::Numerics::BigInteger value);
[System.CLSCompliant(false)]
public static explicit operator ulong (System.Numerics.BigInteger value);
static member op_Explicit : System.Numerics.BigInteger -> uint64
Public Shared Narrowing Operator CType (value As BigInteger) As ULong

参数

value
BigInteger

要转换为 64 位无符号整数的值。The value to convert to an unsigned 64-bit integer.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

属性

异常

value 小于 MinValue 或大于 MaxValuevalue is less than MinValue or is greater than MaxValue.

示例

下面的示例说明 BigIntegerUInt64 值的转换。The following example illustrates the conversion of BigInteger to UInt64 values. 它还处理引发的 OverflowException,因为 BigInteger 值超出了 UInt64 数据类型的范围。It also handles an OverflowException that is thrown because the BigInteger value is outside the range of the UInt64 data type.

// BigInteger to UInt64 conversion.
BigInteger goodULong = 2000000000;
BigInteger badULong = BigInteger.Pow(goodULong, 3);

ulong uLongFromBigInteger;

// Successful conversion using cast operator. 
uLongFromBigInteger = (ulong) goodULong;
Console.WriteLine(uLongFromBigInteger);

// Handle conversion that should result in overflow.
try
{
   uLongFromBigInteger = (ulong) badULong;
   Console.WriteLine(uLongFromBigInteger);
}
catch (OverflowException e)
{
   Console.WriteLine("Unable to convert {0}:\n   {1}", 
                     badULong, e.Message);
}
Console.WriteLine();
' BigInteger to UInt64 conversion.
Dim goodULong As BigInteger = 2000000000
Dim badULong As BigInteger = BigInteger.Pow(goodULong, 3)

Dim uLongFromBigInteger As ULong

' Convert using CType function.
uLongFromBigInteger = CType(goodULong, ULong)
Console.WriteLine(uLongFromBigInteger)
' Convert using CULng function.
uLongFromBigInteger = CULng(goodULong)
Console.WriteLine(uLongFromBigInteger)

' Handle conversion that should result in overflow.
Try
   uLongFromBigInteger = CType(badULong, ULong)
   Console.WriteLine(uLongFromBigInteger)
Catch e As OverflowException
   Console.WriteLine("Unable to convert {0}:{1}   {2}", _
                     badULong, vbCrLf, e.Message)
End Try
Console.WriteLine()

注解

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 语言编译器不会自动执行此转换,因为这可能会导致数据丢失。Language compilers do not perform this conversion automatically because it can involve data loss. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 CTypeCULng 在 Visual Basic 中)时,它们才执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType or CULng in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

由于此操作定义收缩转换,因此如果 BigInteger 值超出 UInt64 数据类型的范围,则它可能会在运行时引发 OverflowExceptionBecause this operation defines a narrowing conversion, it can throw an OverflowException at run time if the BigInteger value is outside the range of the UInt64 data type. 如果转换成功,则结果 UInt64 值中不会有精度损失。There is no loss of precision in the resulting UInt64 value if the conversion is successful.

Explicit(Single to BigInteger)

定义从 Single 值到 BigInteger 值的显式转换。Defines an explicit conversion of a Single value to a BigInteger value.

public:
 static explicit operator System::Numerics::BigInteger(float value);
public static explicit operator System.Numerics.BigInteger (float value);
static member op_Explicit : single -> System.Numerics.BigInteger
Public Shared Narrowing Operator CType (value As Single) As BigInteger

参数

value
Single

要转换为 BigInteger 的值。The value to convert to a BigInteger.

返回

包含 value 参数值的对象。An object that contains the value of the value parameter.

异常

示例

下面的示例将 Single 值数组中的单个元素转换为 BigInteger 对象,然后显示每个转换的结果。The following example converts the individual elements in an array of Single values to BigInteger objects, and then displays the result of each conversion. 请注意,在转换过程中,Single 值的任何小数部分将被截断。Note that any fractional part of a Single value is truncated during the conversion.

float[] singles = { Single.MinValue, -1.430955172e03f, 2.410970032e05f, 
                    Single.MaxValue, Single.PositiveInfinity, 
                     Single.NegativeInfinity, Single.NaN };
BigInteger number;

Console.WriteLine("{0,37} {1,37}\n", "Single", "BigInteger");

foreach (float value in singles)
{
   try {
      number = (BigInteger) value;
      Console.WriteLine("{0,37} {1,37}", value, number);
   }   
   catch (OverflowException) {
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException");
   }
}
// The example displays the following output:
//           Single                            BigInteger
// 
//    -3.402823E+38   -3.4028234663852885981170418348E+38
//        -1430.955                                 -1430
//           241097                                241097
//     3.402823E+38    3.4028234663852885981170418348E+38
//         Infinity                     OverflowException
//        -Infinity                     OverflowException
//              NaN                     OverflowException      
Dim singles() As Single = { Single.MinValue, -1.430955172e03, 2.410970032e05, 
                            Single.MaxValue, Single.PositiveInfinity, 
                            Single.NegativeInfinity, Single.NaN }
Dim number As BigInteger

Console.WriteLine("{0,37} {1,37}", "Single", "BigInteger")
Console.WriteLine()
For Each value As Single In singles
   Try
      number = CType(value, BigInteger)
      Console.WriteLine("{0,37} {1,37}", value, number)
   Catch e As OverflowException
      Console.WriteLine("{0,37} {1,37}", value, "OverflowException")
   End Try
Next     
' The example displays the following output:
'           Single                            BigInteger
' 
'    -3.402823E+38   -3.4028234663852885981170418348E+38
'        -1430.955                                 -1430
'           241097                                241097
'     3.402823E+38    3.4028234663852885981170418348E+38
'         Infinity                     OverflowException
'        -Infinity                     OverflowException
'              NaN                     OverflowException      

注解

在转换前截断 value 参数的任何小数部分。Any fractional part of the value parameter is truncated before conversion.

Explicit(Decimal to BigInteger) 方法的重载定义可以从中转换 BigInteger 对象的类型。The overloads of the Explicit(Decimal to BigInteger) method define the types to which or from which a BigInteger object can be converted. 由于从 SingleBigInteger 的转换可能涉及截断 value的任何小数部分,因此语言编译器不会自动执行此转换。Because the conversion from Single to BigInteger can involve truncating any fractional part of value, language compilers do not perform this conversion automatically. 相反,仅当使用强制转换运算符(在中C#)或转换函数(如 Visual Basic 中的 CType)时,它们才会执行转换。Instead, they perform the conversion only if a casting operator (in C#) or a conversion function (such as CType in Visual Basic) is used. 否则,它们会显示编译器错误。Otherwise, they display a compiler error.

对于不支持自定义运算符的语言,将 BigInteger.BigInteger(Single)替代方法。For languages that do not support custom operators, the alternative method is BigInteger.BigInteger(Single).

适用于