How to get TypeCode in C#?


To get TypeCode in C#, the code is as follows −

Example

 Live Demo

using System;
public class Demo {
   public static void Main(){
      string s = "Demo";
      Console.WriteLine("String = " +s);
      Console.WriteLine("String Type = " +s.GetType());
      Console.WriteLine("GetTypeCode = " +s.GetTypeCode());
   }
}

Output

This will produce the following output −

String = Demo String Type = System.String GetTypeCode = String

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      int i = 100;
      double d = 5.26d;
      Console.WriteLine("Value1 = " +i);
      Console.WriteLine("GetType = " +i.GetType());
      Console.WriteLine("GetTypeCode = " +i.GetTypeCode());
      Console.WriteLine("
Value2 = " +d);       Console.WriteLine("GetType = " +d.GetType());       Console.WriteLine("GetTypeCode = " +d.GetTypeCode());    } }

Output

This will produce the following output −

Value1 = 100
GetType = System.Int32
GetTypeCode = Int32
Value2 = 5.26
GetType = System.Double
GetTypeCode = Double

Updated on: 10-Dec-2019

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements