Open In App

ushort keyword in C#

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Keywords are the words in a language that are used for some internal process or represent some predefined actions. ushort is a keyword that is used to declare a variable which can store an unsigned integer value from the range 0 to 65,535. It is an alias of System.UInt16.

Syntax:

ushort variable_name = value;

ushort keyword occupies 2 bytes (16 bits) space in the memory.

Example:

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

Example 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));
    }
}


Output:

num: 5
Size of a ushort variable: 2

Example 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);
    }
}
}


Output:

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


Last Updated : 22 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads