C# typeof with Examples

The difference between typeof and is operator in C#

In this tutorial, you will learn the difference between typeof and is operator in C#.

C# classes have a mechanism called inheritance. This allows new derived classes to be created by inheriting from a base class. The derived class can be upcast to function as the base class or downcast to the type of the original class.

It is a convenient mechanism, but in complex code, you may wonder, "Can this object be safely retyped?

C# TryParse

In such cases, the typeof and is operator are used.

Both are used to determine the type, but they work a little differently.

Understand the difference between the two and use them appropriately.

typeof

You can use typeof to determine what the class you instantiate is.

To be precise, typeof retrieves the type declaration (Type class) from the class name.

On the other hand, an object class has a GetType() method to obtain its own type declaration.

By comparing the two, it can be determined if the object is of the class specified by typeof.

Sample code:

  1. using System;
  2. namespace CSharp_TypeOf_Example
  3. {
  4. public class Program
  5. {
  6. public static void Main(string[] args)
  7. {
  8. Animal animal = new BigDog();
  9. Console.WriteLine("typeof(Animal) == animal.GetType(): {0}",
  10. typeof(Animal) == animal.GetType());
  11. Console.WriteLine("typeof(Dog) == animal.GetType(): {0}",
  12. typeof(Dog) == animal.GetType());
  13. Console.WriteLine("typeof(BigDog) == animal.GetType(): {0}",
  14. typeof(BigDog) == animal.GetType());
  15. Console.WriteLine("typeof(GreatBigDog) == animal.GetType(): {0}",
  16. typeof(GreatBigDog) == animal.GetType());
  17. }
  18. }
  19. public class Animal
  20. {
  21. }
  22. public class Dog : Animal
  23. {
  24. }
  25. public class BigDog : Dog
  26. {
  27. }
  28. public class GreatBigDog : BigDog
  29. {
  30. }
  31. }

Output:

typeof(Animal) == animal.GetType(): False
typeof(Dog) == animal.GetType(): False
typeof(BigDog) == animal.GetType(): True
typeof(GreatBigDog) == animal.GetType(): False

is operator

The is operator can be used to check if it can be cast.

Unlike typeof, it returns true if the base class name is used.

Sample code:

  1. using System;
  2. namespace CSharp_Is_Operator_Example
  3. {
  4. public class Program
  5. {
  6. public static void Main(string[] args)
  7. {
  8. Animal animal = new BigDog();
  9. Console.WriteLine("animal is Animal: {0}",
  10. animal is Animal);
  11. Console.WriteLine("animal is Dog: {0}",
  12. animal is Dog);
  13. Console.WriteLine("animal is BigDog: {0}",
  14. animal is BigDog);
  15. Console.WriteLine("animal is GreatBigDog: {0}",
  16. animal is GreatBigDog);
  17. }
  18. }
  19. public class Animal
  20. {
  21. }
  22. public class Dog : Animal
  23. {
  24. }
  25. public class BigDog : Dog
  26. {
  27. }
  28. public class GreatBigDog : BigDog
  29. {
  30.  
  31. }
  32. }

Output:

animal is Animal: True
animal is Dog: True
animal is BigDog: True
animal is GreatBigDog: False


See also:
C# List Contains with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables
C# IndexOf() – List, Array, ArrayList, String