C# Type GetTypeCode

Description

Type GetTypeCode gets the underlying type code of the specified Type.

Syntax

Type.GetTypeCode has the following syntax.


public static TypeCode GetTypeCode(
  Type type
)

Parameters

Type.GetTypeCode has the following parameters.

  • type - The type whose underlying type code to get.

Returns

Type.GetTypeCode method returns The code of the underlying type, or Empty if type is null.

Example

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.


/* ww  w . j a  v a  2  s  .c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    object testObject = "java2s.com";
    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;
    }
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version