Int32.GetTypeCode Method in C# with Examples

The Int32.GetTypeCode() method in C# returns the TypeCode enumeration value for the Int32 data type. This method is inherited from the IConvertible interface and is useful for type identification and runtime type checking operations.

Syntax

Following is the syntax −

public TypeCode GetTypeCode();

Return Value

This method returns TypeCode.Int32 for all int values, regardless of their actual numeric value.

Using GetTypeCode() for Type Identification

Example

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

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

Let us see another example using minimum and maximum int values −

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

Practical Use Case for Type Checking

Example

Here's a practical example showing how GetTypeCode() can be used for runtime type checking −

using System;

public class Demo {
    public static void CheckType(object value) {
        if (value is IConvertible convertible) {
            TypeCode typeCode = convertible.GetTypeCode();
            Console.WriteLine($"Value: {value}, TypeCode: {typeCode}");
            
            if (typeCode == TypeCode.Int32) {
                Console.WriteLine("This is a 32-bit integer");
            }
        }
    }
    
    public static void Main() {
        int intVal = 42;
        double doubleVal = 42.5;
        string stringVal = "42";
        
        CheckType(intVal);
        CheckType(doubleVal);
        CheckType(stringVal);
    }
}

The output of the above code is −

Value: 42, TypeCode: Int32
This is a 32-bit integer
Value: 42.5, TypeCode: Double
Value: 42, TypeCode: String

Conclusion

The Int32.GetTypeCode() method returns TypeCode.Int32 for all integer values and is useful for runtime type identification. It's particularly valuable when working with generic code or when you need to determine the exact type of numeric values at runtime.

Updated on: 2026-03-17T07:04:35+05:30

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements