當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。