The typeof is an operator keyword which is used to get a type at the compile-time. Or in other words, this operator is used to get the
System.Type object for a type. This operator takes the Type itself as an argument and returns the marked type of the argument.
- The operand of typeof operator is always a type of parameter or name of the type. It does not contain variable.
- It is not allowed to overload typeof operator.
- It is allowed to use typeof operator on open generic types.
- It is allowed to use typeof operator on bounded or unbounded types.
System.Type type = typeof(int);
Here, type is the type that is obtained.
System.Double System.Int32 System.Array System.Char System.Int32
Difference between typeof operator and GetType method
|typeof Operator||GetType Method|
|It takes the Type itself as an argument and returns the marked type of the argument.||It only invoked on the instance of the type.|
|It is used to get a type that is known at compile-time.||It is used to obtain the type of an object at run-time.|
|It cannot be used on an instance.||It can be used on instance.|
Type b1 = typeof(object); this will return System.Object but
Type b2 = obj.GetType(); will return System.String. As, at compile time only object type reference is created, but at runtime the string(“Hello”) is actually storing in it.
- Is vs As operator keyword in C#
- C# | as Operator Keyword
- C# | is Operator Keyword
- C# | this Keyword
- LINQ | Let Keyword
- Static keyword in C#
- C# | finally keyword
- How to implement is functionality without using is keyword in C#
- Difference between readonly and const keyword in C#
- Invoking an overloaded constructor using this keyword in C#
- LINQ | Set Operator | Except
- C# | Operator Overloading
- LINQ | Quantifier Operator | All
- LINQ | Set Operator | Union
- LINQ | Quantifier Operator | Any
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.