Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Int16 Struct
The Int16 Struct in C# represents a 16-bit signed integer with values ranging from -32,768 to 32,767. It is also commonly known as the short data type and is part of the .NET value types that directly inherit from System.ValueType.
Fields
The Int16 struct provides two important constant fields −
| Field | Description | Value |
|---|---|---|
| MaxValue | Represents the largest possible value of an Int16 | 32,767 |
| MinValue | Represents the smallest possible value of an Int16 | -32,768 |
Common Methods
| Method | Description |
|---|---|
| CompareTo(Int16) | Compares this instance to another 16-bit signed integer |
| Equals(Int16) | Returns true if this instance equals the specified Int16 value |
| GetHashCode() | Returns the hash code for this instance |
| GetTypeCode() | Returns the TypeCode for value type Int16 |
| Parse(String) | Converts string representation to its 16-bit signed integer equivalent |
| ToString() | Converts the numeric value to its string representation |
Using Int16.MaxValue and MinValue
Example
using System;
public class Demo {
public static void Main() {
short val1 = Int16.MinValue;
short val2 = Int16.MaxValue;
short val3 = 0;
Console.WriteLine("MinValue = " + val1);
Console.WriteLine("MaxValue = " + val2);
Console.WriteLine("Zero = " + val3);
Console.WriteLine("Size of Int16: " + sizeof(short) + " bytes");
}
}
The output of the above code is −
MinValue = -32768 MaxValue = 32767 Zero = 0 Size of Int16: 2 bytes
Using GetHashCode() Method
The GetHashCode() method returns a hash code for the current Int16 instance, useful for hash-based collections −
Example
using System;
public class Demo {
public static void Main() {
short val1 = 20;
short val2 = 25;
short val3 = 20;
Console.WriteLine("Value1 = " + val1);
Console.WriteLine("Value2 = " + val2);
Console.WriteLine("Value3 = " + val3);
Console.WriteLine("HashCode for value1 = " + val1.GetHashCode());
Console.WriteLine("HashCode for value2 = " + val2.GetHashCode());
Console.WriteLine("HashCode for value3 = " + val3.GetHashCode());
Console.WriteLine("val1 equals val2? " + val1.Equals(val2));
Console.WriteLine("val1 equals val3? " + val1.Equals(val3));
}
}
The output of the above code is −
Value1 = 20 Value2 = 25 Value3 = 20 HashCode for value1 = 1310740 HashCode for value2 = 1638425 HashCode for value3 = 1310740 val1 equals val2? False val1 equals val3? True
Using GetTypeCode() Method
The GetTypeCode() method returns the TypeCode enumeration value for the Int16 type −
Example
using System;
public class Demo {
public static void Main() {
short val1 = 100;
int val2 = 100;
long val3 = 100;
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
TypeCode type3 = val3.GetTypeCode();
Console.WriteLine("Short value: " + val1 + ", TypeCode: " + type1);
Console.WriteLine("Int value: " + val2 + ", TypeCode: " + type2);
Console.WriteLine("Long value: " + val3 + ", TypeCode: " + type3);
}
}
The output of the above code is −
Short value: 100, TypeCode: Int16 Int value: 100, TypeCode: Int32 Long value: 100, TypeCode: Int64
Using Parse() Method
Example
using System;
public class Demo {
public static void Main() {
string str1 = "1500";
string str2 = "-2500";
short result1 = Int16.Parse(str1);
short result2 = Int16.Parse(str2);
Console.WriteLine("Parsed '" + str1 + "' to: " + result1);
Console.WriteLine("Parsed '" + str2 + "' to: " + result2);
Console.WriteLine("Sum: " + (result1 + result2));
}
}
The output of the above code is −
Parsed '1500' to: 1500 Parsed '-2500' to: -2500 Sum: -1000
Conclusion
The Int16 struct in C# provides a 16-bit signed integer type with a range from -32,768 to 32,767. It offers essential methods for comparison, equality checking, hash code generation, and type conversion, making it suitable for memory-efficient integer operations where the value range fits within 16 bits.
