In this short, we will learn to use the GetType()
method to return the instance of a Type
class value. In C#, we can get the instance of any value easily.
Note: One instance class we might be familiar with includes the
String
class.
public Type GetType()
It takes no parameters.
This method returns an object of Type
class.
In the example below, we create several variables and their instances are obtained by using the GetType()
method:
using System; // create our class class TypeGetter { // main method static void Main() { // create variables string name = "Burger"; int id = 20; char label = 'B'; double price = 200.00; // printout the instances Console.WriteLine(name.GetType()); Console.WriteLine(price.GetType()); Console.WriteLine(label.GetType()); Console.WriteLine(id.GetType()); } }
From the code above, we use the GetType()
method to get instances of the variables or objects we created.
RELATED TAGS
CONTRIBUTOR
View all Courses