Skip to content
Related Articles

Related Articles

C# | UInt16 Struct
  • Last Updated : 01 May, 2019

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

FieldDescription
MaxValueRepresents the largest possible value of UInt16. This field is constant.
MinValueRepresents the smallest possible value of UInt16. This field is constant.

Example:




// 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");
        }
    }
}
Output:
Not equal

Methods

MethodDescription
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:




// 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);
    }
}
Output:
The hash code of myval is: 545

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :