Single.GetTypeCode Method in C# with Examples

The Single.GetTypeCode() method in C# is used to return the TypeCode for the Single value type (also known as float). This method is inherited from the IConvertible interface and always returns TypeCode.Single for any float value.

Syntax

Following is the syntax for the Single.GetTypeCode() method −

public TypeCode GetTypeCode();

Return Value

This method returns TypeCode.Single, which is an enumerated constant representing the Single data type.

Using GetTypeCode with Different Float Values

Example

using System;

public class Demo {
   public static void Main() {
      float f1 = 15.3f;
      float f2 = 35.9f;
      Console.WriteLine("Value1 = " + f1);
      Console.WriteLine("Hashcode for Value1 = " + f1.GetHashCode());
      Console.WriteLine("TypeCode for Value1 = " + f1.GetTypeCode());
      Console.WriteLine("\nValue2 = " + f2);
      Console.WriteLine("Hashcode for Value2 = " + f2.GetHashCode());
      Console.WriteLine("TypeCode for Value2 = " + f2.GetTypeCode());
      Console.WriteLine("\nIs f1 and f2 equal? = " + f1.CompareTo(f2));
   }
}

The output of the above code is −

Value1 = 15.3
Hashcode for Value1 = 1098173645
TypeCode for Value1 = Single

Value2 = 35.9
Hashcode for Value2 = 1108318618
TypeCode for Value2 = Single

Is f1 and f2 equal? = -1

Using GetTypeCode with Special Float Values

Example

using System;

public class Demo {
   public static void Main() {
      float f1 = 19.3f;
      float f2 = Single.MaxValue;
      Console.WriteLine("Value1 = " + f1);
      Console.WriteLine("Hashcode for Value1 = " + f1.GetHashCode());
      Console.WriteLine("TypeCode for Value1 = " + f1.GetTypeCode());
      Console.WriteLine("\nValue2 = " + f2);
      Console.WriteLine("Hashcode for Value2 = " + f2.GetHashCode());
      Console.WriteLine("TypeCode for Value2 = " + f2.GetTypeCode());
   }
}

The output of the above code is −

Value1 = 19.3
Hashcode for Value1 = 1100637798
TypeCode for Value1 = Single

Value2 = 3.402823E+38
Hashcode for Value2 = 2139095039
TypeCode for Value2 = Single

TypeCode for Different Numeric Types

Example

using System;

public class Demo {
   public static void Main() {
      float floatValue = 25.5f;
      double doubleValue = 25.5;
      int intValue = 25;
      decimal decimalValue = 25.5m;

      Console.WriteLine("Float TypeCode: " + floatValue.GetTypeCode());
      Console.WriteLine("Double TypeCode: " + doubleValue.GetTypeCode());
      Console.WriteLine("Int TypeCode: " + intValue.GetTypeCode());
      Console.WriteLine("Decimal TypeCode: " + decimalValue.GetTypeCode());
   }
}

The output of the above code is −

Float TypeCode: Single
Double TypeCode: Double
Int TypeCode: Int32
Decimal TypeCode: Decimal

Conclusion

The Single.GetTypeCode() method consistently returns TypeCode.Single for all float values, regardless of their actual numeric value. This method is useful for type checking and conversion operations when working with different numeric data types in C#.

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

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements