📜  C#中的ushort关键字

📅  最后修改于: 2021-05-29 22:01:24             🧑  作者: Mango

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

句法:

ushort variable_name = value;

ushort关键字在内存中占用2个字节(16位)的空间。

例子:

Input: num: 5

Output: num: 5
        Size of a ushort variable: 2

Input: num = 8765

Output: num: 8765
        Type of num: System.UInt16
        Size of a ushort variable: 2

范例1:

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

输出:

num: 5
Size of a ushort variable: 2

范例2:

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

输出:

num: 8765
Type of num: System.UInt16
Size of a ushort variable: 2
Min value of ushort: 0
Max value of ushort: 65535