Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
typecode
object
communitycreator

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

Theodore Kelechukwu Onyejiaku

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());
    }
}
Get the TypeCode of object values in C#

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.

RELATED TAGS

c#
typecode
object
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring