How to get the TypeCode of an object in C#?

Overview

In C#, we can use the GetTypeCode() method to get the TypeCode of any object value. This method returns the TypeCode of the object that calls it. A TypeCode is used to identify the type of an object.

Snytax

public TypeCode GetTypeCode ();

Parameters

This method takes no parameter(s). It is only called on an object.

Return value

This method returns a TypeCode of the object that calls it.

Code example

// create a class
class ObjectTypeCode
{
// main method
static void Main()
{
// create decimal values
decimal decimalNumberOne = 123.4m;
decimal decimalNumberTwo = 34.5555m;
// create integer values
int integerNumberOne = 234;
int integerNumberTwo = 25;
// create string values
string stringValueOne = "Edpresso";
string stringValueTwo = "C#";
// create boolean values
bool booleanValueOne = false;
bool booleanValueTwo = true;
// get typecode
System.Console.WriteLine(decimalNumberOne.GetTypeCode());
System.Console.WriteLine(decimalNumberTwo.GetTypeCode());
System.Console.WriteLine(integerNumberOne.GetTypeCode());
System.Console.WriteLine(integerNumberTwo.GetTypeCode());
System.Console.WriteLine(stringValueOne.GetTypeCode());
System.Console.WriteLine(stringValueTwo.GetTypeCode());
System.Console.WriteLine(booleanValueOne.GetTypeCode());
System.Console.WriteLine(booleanValueTwo.GetTypeCode());
}
}

Code explanation

  • Lines 8–9: We create some decimal variables and initialize them with values.

  • Lines 12–13: We created some integer variables and initialize them.

  • Lines 16–17: We create some string variables and initialize them.

  • Lines 20–21: We create some Boolean variables and initialize them.

When we run the code written above, we discover that the results show the instance types of the objects we created.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources