What is typeof, GetType or is in C#?


Typeof()

The type takes the Type and returns the Type of the argument.

GetType()

The GetType() method of array class in C# gets the Type of the current instance.

is

The "is" keyword is used to check if an object can be casted to a specific type. The return type of the operation is Boolean.

Example

 Live Demo

class Demo {
}
class Program {
   static void Main() {
      var demo = new Demo();
      Console.WriteLine($"typeof { typeof(Demo)}");
      Type tp = demo.GetType();
      Console.WriteLine($"GetType {tp}");
      if (demo is Demo) {
         System.Console.WriteLine($"is keyword check true");
      }
      Console.ReadLine();
   }
}

Output

typeof ConsoleApp.Demo
GetType ConsoleApp.Demo
is keyword check true

Updated on: 08-Aug-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements