Edit

Share via


Type.GetTypeCode(Type) Method

Definition

Gets the underlying type code of the specified Type.

C#
public static TypeCode GetTypeCode(Type? type);
C#
public static TypeCode GetTypeCode(Type type);

Parameters

type
Type

The type whose underlying type code to get.

Returns

The code of the underlying type, or Empty if type is null.

Examples

The following code example demonstrates how the TypeCode enumeration can be used. In a decision block inside the WriteObjectInfo method, the TypeCode of an Object parameter is examined, and an appropriate message is written to the console.

C#
static void WriteObjectInfo(object testObject)
{
    TypeCode    typeCode = Type.GetTypeCode( testObject.GetType() );

    switch( typeCode )
    {
        case TypeCode.Boolean:
            Console.WriteLine("Boolean: {0}", testObject);
            break;

        case TypeCode.Double:
            Console.WriteLine("Double: {0}", testObject);
            break;

        default:
            Console.WriteLine("{0}: {1}", typeCode.ToString(), testObject);
            break;
    }
}

Remarks

When you inherit from Type, you can change the behavior of this method by overriding the GetTypeCodeImpl method. For Enum types, the type code of the underlying integral type is returned.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1

See also