C# | UInt16 Struct

In C#, UInt16 struct is used to represent 16-bit unsigned integers(also termed as ushort data type) starting from range 0 to 65535. It provides different types of method to perform various actions like to compare instances of this type, convert the value of an instance to its string representation, convert the string representation of a number to an instance of this type, etc. This struct is defined under System namespace. UInt16 struct inherits the ValueType class which inherits the Object class.

Fields

Field Description
MaxValue Represents the largest possible value of UInt16. This field is constant.
MinValue Represents the smallest possible value of UInt16. This field is constant.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// fields of UInt16 struct
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Unsigned 16-bit integer
        ushort val = 295;
  
        // Checking the unsigned integer
        if (val.Equals(UInt16.MinValue)) 
        {
            Console.WriteLine("Equal to MinValue..!");
        }
  
        else if (val.Equals(UInt16.MaxValue)) 
        {
            Console.WriteLine("Equal to MaxValue");
        }
  
        else 
        {
            Console.WriteLine("Not equal");
        }
    }
}

chevron_right


Output:



Not equal

Methods

Method Description
CompareTo() Compares the current instance to a specified object or UInt16 and returns an indication of their relative values.
Equals() Returns a value which shows whether the current instance is equal to a specified object or UInt16.
GetHashCode() Returns the hash code for this instance.
GetTypeCode() Returns the TypeCode for value type UInt16.
Parse() Converts the string representation of a number to its 16-bit unsigned integer equivalent.
ToString() Converts the numeric value of this instance to its equivalent string representation.
TryParse() Converts the string representation of a number to its 16-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to get the hash 
// code of the 16-bit Unsigned integer
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // UInt16 variable
        ulong myval = 545;
  
        // Get the hash code
        // Using GetHashCode Method
        int res = myval.GetHashCode();
  
        Console.WriteLine("The hash code of myval is: {0}", res);
    }
}

chevron_right


Output:

The hash code of myval is: 545

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.