How to get the instance of a value with GetType() in C#

Overview

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.

Syntax

public Type GetType()

Parameter

It takes no parameters.

Return Value

This method returns an object of Type class.

Example

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.

Free Resources