Getting the type of the current instance in C#


To get the type of the current instance, 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());
   }
}

Output

This will produce the following output −

String = Demo
String Type = System.String

Example

Let us now see another example −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      double val1 = 5.5;
      int val2 = 10;
      short val3 = 2;
      Console.WriteLine("Value = " +val1);
      Console.WriteLine("Value Type = " +val1.GetType());
      Console.WriteLine("Value = " +val2);
      Console.WriteLine("Value Type = " +val2.GetType());
      Console.WriteLine("Value = " +val3);
      Console.WriteLine("Value Type = " +val3.GetType());
   }
}

Output

This will produce the following output −

Value = 5.5
Value Type = System.Double
Value = 10 Value
Type = System.Int32
Value = 2 Value
Type = System.Int16

Updated on: 06-Dec-2019

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements