Table of contents
1.
Introduction
2.
typeof Keyword
3.
Features
4.
Example
5.
Difference between typeof operator and GetType method
6.
Uses
7.
Frequently Asked Questions
7.1.
What is the typeof operator?
7.2.
What is the GetType method?
7.3.
What are keywords?
7.4.
Is overloading the typeof operator permitted?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

TypeOf Operator Keyword in C#

Author Nagendra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C# (pronounced as c sharp) is a programming language built by Microsoft and operates on the.Net framework. It is closely related to the C programming language, as well as other computer languages such as C++ and Java. Its most impressive feature is its reusability. The language is simple to learn and comprehend. Keywords are reserved words that have a special meaning for the compiler. These terms cannot be used as identifiers for variables, classes, or anything else. One such keyword is typeof we would be discussing typeof keyword in this blog.

So, without any further ado let's get started.

Click on the following link to read further: Features of C Language

typeof Keyword


The typeof keyword is used to get a type at compile time. Alternatively, this operator is used to obtain the System.Type object. This operator takes the Type as an argument and returns the argument's marked type. Syntax of typeof is as follows

Syntax:

System.Type type = typeof(int);

Here, type is the type that is obtained

Features

The following are the main features of typeof operator keyword :

  • The typeof operator's operand is always a type of argument or a type name.
  • Overloading the typeof operator is not permitted.
  • The typeof operator can be used on open generic types.
  • The typeof operator can be used on both bounded and unbounded types.

Example

Consider the following code snippet:

using System;
using System.IO;
class Ninja
{
    static Type type = typeof(char); // Store Type as field
    static void Main()
    {
		Console.WriteLine(type); // Value type pointer
		Console.WriteLine(typeof(int)); // Value type
		Console.WriteLine(typeof(byte)); // Value type
		Console.WriteLine(typeof(Stream)); // Class type
		Console.WriteLine(typeof(TextWriter)); // Class type
		Console.WriteLine(typeof(Array)); // Class type
		Console.WriteLine(typeof(int[])); // Array reference type
    }
}

Output

System.CharSystem.Int32System.Byte
System.IO.Stream
System.IO.TextWriter
System.Array
System.Int32[]

Difference between typeof operator and GetType method

One very similar method to typeof keyword is GetType method so to avoid any sort of confusion let's discuss the difference between the both.

The following chart illustrates the difference between typeof operator and the GetType method :

Uses

The typeof operator is used to obtain a compile-time type, whereas GetType determines an object's type at runtime.
Consider the following example :

using System;
class ninja {
static public void Main()
{
string s = "Ninja";
Type a1 = typeof(string); //String type
Type a2 = s.GetType(); //GetType method
Console.WriteLine(a1 == a2); //equality Checking
object obj = "Hello";
Type b1 = typeof(object); //typeof operator
Type b2 = obj.GetType(); //GetType method
Console.WriteLine(b1 == b2); //the GetType method is used to get the run-time type, it returns False.
}
}

Output:

True
False

Explanation:

In the above code snippet , Type b1 = typeof(object) returns System.Object in this case. However, Type b2 = obj.GetType() returns System.String. It is because only an object type reference is established at compile-time, but the string("Hello") is really stored in it at runtime.

Must Read IEnumerable vs IQueryable.

Frequently Asked Questions

What is the typeof operator?

The typeof keyword is an operator used to get a type at compile time. 

What is the GetType method?

The GetType method is used to get a type at runtime. 

What are keywords?

Keywords are reserved words that have a special meaning for the compiler. These terms cannot be used as identifiers for variables, classes, or anything else.

Is overloading the typeof operator permitted?

Overloading the typeof operator is not permitted.

Conclusion

In this article, we have extensively discussed the TypeOf Operator Keyword in C#. The article explains the details of the TypeOf Operator Keyword in C# and the difference between typeof operator and GetType method with examples.
We hope that this blog has helped you enhance your knowledge regarding TypeOf Operator Keyword in C# and if you would like to learn more, check out our articles on C#. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Recommended Readings:

Singleton Design Pattern C#.

C# interview questions for 5 years Experience

Introduction to C#

Do upvote our blog to help other ninjas grow. 

Happy Coding!!

Live masterclass