UInt16 Struct in C#


The UInt16 struct represents a 16-bit unsigned integer. The UInt16 value type represents unsigned integers with values ranging from 0 to 65535.

Let us now see some examples of UInt16 Struct methods −

UInt16.CompareTo()

The UInt16.CompareTo() method in C# is used to compare the current instance to a specified object or UInt16 and returns an indication of their relative values.

Syntax

Following is the syntax −

public int CompareTo (object val);
public int CompareTo (ushort val;

Above, the value for the 1st syntax is an object to compare. The value for 2nd syntax is an unsigned integer to compare.

The return value is 0 if the current instance is equal to value. It’s less than zero if the current instance is less than Val. The return value is more than zero if the current instance is greater than the value.

Example

Let us now see an example to implement the UInt16.CompareTo() method −

using System;
public class Demo {
   public static void Main(){
      ushort val1 = 25;
      ushort val2 = 55;
      int res = val1.CompareTo(val2);
      Console.WriteLine("Return value (comparison) = "+res);
      if (res > 0)
         Console.WriteLine("val1 > val2");
      else if (res < 0)
         Console.WriteLine("val1 < val2");
      else
         Console.WriteLine("val=val2");
   }
}

Output

This will produce the following output −

Return value (comparison) = -30
val1 < val2

Example

Let us now see another example to implement the UInt16.CompareTo() method −

using System;
public class Demo {
   public static void Main(){
      ushort val1 = 25;
      object val2 = (ushort)2;
      int res = val1.CompareTo(val2);
      Console.WriteLine("Return value (comparison) = "+res);
      if (res > 0)
         Console.WriteLine("val1 > val2");
      else if (res < 0)
         Console.WriteLine("val1 < val2");
      else
         Console.WriteLine("val=val2");
   }
}

Output

This will produce the following output −

Return value (comparison) = 23
val1 > val2

UInt16.Equals()

The UInt16.Equals() method in C# returns a value indicating whether this instance is equal to a specified object or UInt16.

Syntax

Following is the syntax −

public override bool Equals (object ob);
public bool Equals (ushort ob);

Above, the parameter ob for the 1st syntax is an object to compare to this instance and the parameter ob for the 2nd syntax is the 16-bit unsigned integer to compare to this instance.

Example

Let us now see an example to implement the UInt16.Equals() method −

using System;
public class Demo {
   public static void Main(){
      ushort val1 = 52;
      ushort val2 = 10;
      bool res = val1.Equals(val2);
      Console.WriteLine("Return value (comparison) = "+res);
      if (res)
         Console.WriteLine("val1 = val2");
      else
         Console.WriteLine("val1 != val2");
   }
}

Output

This will produce the following output −

Return value (comparison) = False
val1 != val2

Example

Let us now see another example to implement the UInt16.Equals() method −

using System;
public class Demo {
   public static void Main(){
      ushort val1 = 100;
      ushort val2 = 100;
      bool res = val1.Equals(val2);
      Console.WriteLine("Return value (comparison) = "+res);
      if (res)
         Console.WriteLine("val1 = val2");
      else
         Console.WriteLine("val1 != val2");
   }
}

Output

This will produce the following output −

Return value (comparison) = True
val1 = val2
raja
Published on 08-Nov-2019 07:13:52
Advertisements