Int64.GetTypeCode Method in C# with Examples

The Int64.GetTypeCode() method in C# is used to return the TypeCode for value type Int64. This method is inherited from the IConvertible interface and returns TypeCode.Int64 for all 64-bit signed integer values.

Syntax

Following is the syntax −

public TypeCode GetTypeCode();

Return Value

This method returns TypeCode.Int64, which is the enumerated constant representing the Int64 type.

Using GetTypeCode() with Different Values

Example

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

using System;

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

The output of the above code is −

Value1 = 0
Value2 = 9223372036854775807
Are they equal? = False
Value1 (HashCode) = 0
Value2 (HashCode) = -2147483648
TypeCode for val1 = Int64
TypeCode for val2 = Int64

Using GetTypeCode() with Custom Values

Example

Let us see another example with different Int64 values −

using System;

public class Demo {
   public static void Main() {
      long val1 = 8768768766;
      long val2 = Int64.MinValue;
      long val3 = -12345;
      
      Console.WriteLine("Value1 = " + val1);
      Console.WriteLine("Value2 = " + val2);
      Console.WriteLine("Value3 = " + val3);
      
      Console.WriteLine("TypeCode for val1 = " + val1.GetTypeCode());
      Console.WriteLine("TypeCode for val2 = " + val2.GetTypeCode());
      Console.WriteLine("TypeCode for val3 = " + val3.GetTypeCode());
      
      Console.WriteLine("All TypeCodes are equal: " + 
         (val1.GetTypeCode() == val2.GetTypeCode() && 
          val2.GetTypeCode() == val3.GetTypeCode()));
   }
}

The output of the above code is −

Value1 = 8768768766
Value2 = -9223372036854775808
Value3 = -12345
TypeCode for val1 = Int64
TypeCode for val2 = Int64
TypeCode for val3 = Int64
All TypeCodes are equal: True

TypeCode Comparison

Example

This example demonstrates how GetTypeCode() differs across various numeric types −

using System;

public class Demo {
   public static void Main() {
      long longVal = 100L;
      int intVal = 100;
      short shortVal = 100;
      byte byteVal = 100;
      
      Console.WriteLine("Long value: " + longVal + " - TypeCode: " + longVal.GetTypeCode());
      Console.WriteLine("Int value: " + intVal + " - TypeCode: " + intVal.GetTypeCode());
      Console.WriteLine("Short value: " + shortVal + " - TypeCode: " + shortVal.GetTypeCode());
      Console.WriteLine("Byte value: " + byteVal + " - TypeCode: " + byteVal.GetTypeCode());
   }
}

The output of the above code is −

Long value: 100 - TypeCode: Int64
Int value: 100 - TypeCode: Int32
Short value: 100 - TypeCode: Int16
Byte value: 100 - TypeCode: Byte

Conclusion

The Int64.GetTypeCode() method always returns TypeCode.Int64 regardless of the actual value of the 64-bit signed integer. This method is useful for type checking and conversion operations in generic programming scenarios.

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

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements