UInt32 Struct的MaxValue字段用于表示32位无符号整数的最大值。该字段的值是常数,表示用户无法更改该字段的值。该字段的值为4294967295。其十六进制值为0xFFFFFFFF 。通过在类型转换之前验证Int64值是否在UInt32类型的范围内,可以避免运行时发生OverflowException。
句法:
public const uint MaxValue = 4294967295;
返回值:该字段始终返回4294967295。
例子:
// C# program to illustrate the
// UInt32.MaxValue field
using System;
class GFG {
// Main Method
static public void Main()
{
// display the Maximum value
// of UInt32 struct
Console.WriteLine("Maximum Value is: " + UInt32.MaxValue);
// Taking an array of the
// unsigned long integer
// i.e UInt64 data type
ulong[] num = {3422146, 732443, 42343535776, 2342534654756123};
// taking variable of UInt32 type
uint mynum;
foreach(long n in num)
{
if (n >= UInt32.MinValue && n <= UInt32.MaxValue)
{
// using the method of Convert class
// to convert Int64 to UInt32
mynum = Convert.ToUInt32(n);
Console.WriteLine("Conversion is Possible.");
}
else
{
Console.WriteLine("Not Possible");
}
}
}
}
输出:
Maximum Value is: 4294967295
Conversion is Possible.
Conversion is Possible.
Not Possible
Not Possible
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.uint32.maxvalue?view=netstandard-2.1