ushort keyword in C#

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

num: 5
Size of a ushort variable: 2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.