C# Int16 Struct


The Int16 Struct represents a 16-bit signed integer with values ranging from negative 32768 through positive 32767.

Following are the fields of Int16 −

Sr.NoField & Description
1MaxValue  Represents the largest possible value of an Int16. This field is constant.
2MinValue  Represents the smallest possible value of an Int16. This field is constant.

Following are some of the methods −

Sr.NoMethod & Description
1CompareTo(Int16) − Compares this instance to a specified 16-bit signed integer and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified 16-bit signed integer.
2CompareTo(Object) − Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the object.
3Equals(Int16)  Returns a value indicating whether this instance is equal to a specified Int16 value.
4Equals(Object)  Returns a value indicating whether this instance is equal to a specified object.
5GetHashCode()  Returns the hash code for this instance.
6GetTypeCode()  Returns the TypeCode for value type Int16.
7Parse(String)  Converts the string representation of a number to its 16-bit signed integer equivalent.

Let us now see some examples of the Int16 Struct −

The Int16.GetHashCode() method in C# is used to return the hash code for the current instance.

Syntax

public override int GetHashCode ();

Example

Let us now see an example to implement the Int16.GetHashCode() method −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      short val1 = 20;
      short val2 = 25;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
   }
}

Output

This will produce the following output −

Value1 = 20
Value2 = 25
HashCode for value1 = 1310740
HashCode for value2 = 1638425
Are they equal? = False

Example

Let us now see another example to implement the Int16.GetHashCode() method −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      short val1 = 0;
      short val2 = Int16.MaxValue;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
   }
}

Output

This will produce the following output −

Value1 = 0
Value2 = 32767
HashCode for value1 = 0
HashCode for value2 = 2147450879
Are they equal? = False

The Int16.GetTypeCode() method in C# is used to return the TypeCode for value type Int16.

Syntax

public TypeCode GetTypeCode ();

Example

Let us now see an example to implement the Int16.GetTypeCode() method −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      short val1 = 0;
      short val2 = Int16.MaxValue;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = "+type1);
      Console.WriteLine("TypeCode for val2 = "+type2);
   }
}

Output

This will produce the following output −

Value1 = 0
Value2 = 32767
HashCode for value1 = 0
HashCode for value2 = 2147450879
Are they equal? = False
TypeCode for val1 = Int16
TypeCode for val2 = Int16

Example

Let us now see another example to implement the Int16.GetTypeCode() method −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      short val1 = 23;
      short val2 = 0;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("HashCode for value1 = "+val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = "+val2.GetHashCode());
      Console.WriteLine("Are they equal? = "+(val1.Equals(val2)));
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = "+type1);
      Console.WriteLine("TypeCode for val2 = "+type2);
   }
}

Output

This will produce the following output −

Value1 = 23
Value2 = 0
HashCode for value1 = 1507351
HashCode for value2 = 0
Are they equal? = False
TypeCode for val1 = Int16
TypeCode for val2 = Int16

Updated on: 04-Dec-2019

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements