当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# UInt32.MaxValue用法及代码示例


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

参考:



相关用法


注:本文由纯净天空筛选整理自ankita_saini大神的英文原创作品 UInt32.MaxValue Field in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。