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.
public TypeCode GetTypeCode ();
This method takes no parameter(s). It is only called on an object.
This method returns a TypeCode of the object that calls it.
// create a classclass ObjectTypeCode{// main methodstatic void Main(){// create decimal valuesdecimal decimalNumberOne = 123.4m;decimal decimalNumberTwo = 34.5555m;// create integer valuesint integerNumberOne = 234;int integerNumberTwo = 25;// create string valuesstring stringValueOne = "Edpresso";string stringValueTwo = "C#";// create boolean valuesbool booleanValueOne = false;bool booleanValueTwo = true;// get typecodeSystem.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());}}
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.