📜  C#中的ulong关键字

📅  最后修改于: 2021-05-29 14:40:27             🧑  作者: Mango

关键字是用于某些内部过程或表示某些预定义动作的语言中的单词。 ulong是一个关键字,用于声明一个变量,该变量可以存储从018,446,744,073,709,551,615范围内的无符号整数值。它是System.UInt64的别名。

ulong关键字在内存中占用8个字节(64位)的空间。

句法:

ulong variable_name = value;

例子:

Input: num: 223

Output: num: 223
        Size of a ulong variable: 8

Input: num = 0

Output: Type of num: System.UInt64
        num: 0
        Size of a ulong variable: 8

笔记:

  • 如果我们输入的数字超出范围,则会显示错误-
    Integral constant is too large
  • 如果我们输入错误的值,例如。 -34,它显示错误-
    Constant value `-34' cannot be converted to a `ulong'
    
    

范例1:

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

输出:

num: 223
Size of a ulong variable: 8

范例2:

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

输出:

Type of num: System.UInt64
num: 0
Size of a ulong variable: 8
Min value of ulong: 0
Max value of ulong: 18446744073709551615