📜  C#中的uint关键字

📅  最后修改于: 2021-05-29 20:13:12             🧑  作者: Mango

关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 uint是一个关键字,用于声明一个变量,该变量可以存储从04,294,967,295范围内的整数类型的值(无符号整数)。 It关键字是System.UInt32的别名。

uint关键字在内存中占用4个字节(32位)的空间。

句法:

uint variable_name = value;

例子:

Input: num: 67

Output: num2: 67
        Size of a uint variable: 4

Input: num = 24680

Output: Type of num1: System.UInt32
        num1: 24680
        Size of a uint variable: 4

范例1:

// C# program for uint keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // variable declaration
        uint num2 = 67;
  
        // to print value
        Console.WriteLine("num2: " + num2);
  
        // to print size
        Console.WriteLine("Size of a uint variable: " + sizeof(uint));
    }
}

输出:

num2: 67
Size of a uint variable: 4

范例2:

// C# program for uint keyword
using System;
using System.Text;
  
namespace Geeks {
  
class GFG {
  
    static void Main(string[] args)
    {
        // variable declaration
        uint num1 = 24680;
  
        // to print type of variable
        Console.WriteLine("Type of num1: " + num1.GetType());
  
        // to print value
        Console.WriteLine("num1: " + num1);
  
        // to print size
        Console.WriteLine("Size of a uint variable: " + sizeof(uint));
  
        // to print minimum & maximum value of uint
        Console.WriteLine("Min value of uint: " + uint.MinValue);
        Console.WriteLine("Max value of uint: " + uint.MaxValue);
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

输出:

Type of num1: System.UInt32
num1: 24680
Size of a uint variable: 4
Min value of uint: 0
Max value of uint: 4294967295

范例3:

// C# program for uint keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // variable declaration
        uint num2 = 4294967297;
  
        // to print type of variable
        Console.WriteLine("Type of num2: " + num2.GetType());
  
        // to print value
        Console.WriteLine("num2: " + num2);
  
        // to print size
        Console.WriteLine("Size of a uint variable: " + sizeof(uint));
    }
}

错误:输入错误的整数并且输入数字超出范围时