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
Get the TypeCode for value type Int32 in C#
The TypeCode enumeration in C# represents the type of an object. For Int32 values, the GetTypeCode() method returns TypeCode.Int32, which identifies the value as a 32-bit signed integer.
The GetTypeCode() method is inherited from the Object class and implemented by value types to return their specific type identifier.
Syntax
Following is the syntax for getting the TypeCode of an Int32 value −
TypeCode typeCode = intValue.GetTypeCode();
Return Value
The GetTypeCode() method returns TypeCode.Int32 for all int variables, regardless of their actual numeric value.
Using GetTypeCode() with Int32 Values
Example
using System;
public class Demo {
public static void Main() {
int val1 = 100;
int val2 = 50;
Console.WriteLine("Value1 = " + val1);
Console.WriteLine("Value2 = " + val2);
Console.WriteLine("Are they equal? = " + val1.Equals(val2));
Console.WriteLine("HashCode for Value1 = " + val1.GetHashCode());
Console.WriteLine("HashCode for Value2 = " + val2.GetHashCode());
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
Console.WriteLine("TypeCode for Value1 = " + type1);
Console.WriteLine("TypeCode for Value2 = " + type2);
}
}
The output of the above code is −
Value1 = 100 Value2 = 50 Are they equal? = False HashCode for Value1 = 100 HashCode for Value2 = 50 TypeCode for Value1 = Int32 TypeCode for Value2 = Int32
Using GetTypeCode() with Extreme Values
Example
using System;
public class Demo {
public static void Main() {
int val1 = 5;
int val2 = Int32.MaxValue;
int val3 = Int32.MinValue;
Console.WriteLine("Value1 = " + val1);
Console.WriteLine("Value2 = " + val2);
Console.WriteLine("Value3 = " + val3);
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
TypeCode type3 = val3.GetTypeCode();
Console.WriteLine("TypeCode for Value1 = " + type1);
Console.WriteLine("TypeCode for Value2 = " + type2);
Console.WriteLine("TypeCode for Value3 = " + type3);
}
}
The output of the above code is −
Value1 = 5 Value2 = 2147483647 Value3 = -2147483648 TypeCode for Value1 = Int32 TypeCode for Value2 = Int32 TypeCode for Value3 = Int32
Comparison with Other Numeric Types
| Data Type | TypeCode | Range |
|---|---|---|
| byte | TypeCode.Byte | 0 to 255 |
| short | TypeCode.Int16 | -32,768 to 32,767 |
| int | TypeCode.Int32 | -2,147,483,648 to 2,147,483,647 |
| long | TypeCode.Int64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Example
using System;
public class Demo {
public static void Main() {
byte byteVal = 255;
short shortVal = 32000;
int intVal = 100000;
long longVal = 5000000000L;
Console.WriteLine("Byte TypeCode: " + byteVal.GetTypeCode());
Console.WriteLine("Short TypeCode: " + shortVal.GetTypeCode());
Console.WriteLine("Int TypeCode: " + intVal.GetTypeCode());
Console.WriteLine("Long TypeCode: " + longVal.GetTypeCode());
}
}
The output of the above code is −
Byte TypeCode: Byte Short TypeCode: Int16 Int TypeCode: Int32 Long TypeCode: Int64
Conclusion
The GetTypeCode() method for Int32 values consistently returns TypeCode.Int32, regardless of the actual numeric value. This method is useful for type identification and comparison in generic programming scenarios where you need to determine the specific type of a value at runtime.
