Illustration of Operator Keyword typeof with Examples in C# (C Sharp)

In this article, you will understand about how to determine the type of a value type or any open generic types using typeof operator keyword of C#. The article doesn’t stop with that; it also suggests an alternative solution to typeof operator which helps you in determining the type of an expression during runtime.

As stated above, typeof operator is used to determine the type and returns the corresponding System.Type object associated with this type. To start with, here is a simple example illustrating the usage of this operator:

class sampleClass {
public static void Main(){
Console.WriteLine(“Type of int is :” + typeof(int));
Console.WriteLine(“Type of char is :” + typeof(char));
Console.WriteLine(“Type of TextWriter is :” + typeof(TextWriter));
}
}

Output of this code will be:
Type of int is System.Int32
Type of char is System.Char
Type of TextWriter is System.IO.TextWriter

Note that the types determined by typeof belong to either System namespace or to the System.IO namespace. Usage of typeof operator has few limitations which are listed below:

• It can never be overloaded
• It deals with public types only

In the above example, you have displayed the type of value types directly. What if you have to store the type in a variable and use it at a later point of time? For that, you can store the result in a type variable. The above example can be modified to store the types as shown below:

class sampleClass {
public static void Main(){
static Type typeOfInt = typeof(int);
static Type typeOfChar= typeof(char);
static Type typeOfTextWriter = typeof(TextWriter);
Console.WriteLine(“Type of int is :” + typeOfInt);
Console.WriteLine(“Type of char is :” + typeOfChar);
Console.WriteLine(“Type of TextWriter is :” + typeOfTextWriter);
}
}

Output of this code will be same as that of the earlier example. You might wonder why to store the result in a type variable. This is because if you want to perform the same type operation multiple times, that will be a performance hit. Instead you can record it in a type variable once and directly use the type variable wherever appropriate. Declaring the type variable as static is yet another way of improvising the performance.

In the examples discussed so far, typeof operator is used to determine value types. Other than that, you can also determine the type of open generic types like class using this operator. Here is a corresponding code sample:

class sampleClass {
public int intVar;
public double doubleVar;
public void setData(int intVar, double doubleVar) {
this.intVar = intVar;
this.doubleVar = doubleVar;
}
public void displayData(){
Console.WriteLine(“intVar:{0}, doubleVar:{1}”, intVar, doubleVar);
}
public static void Main() {
Console.WriteLine(“Type of sampleClass is:” + typeof(sampleClass);
}
}

If you store the type information of a class in a type variable, you can retrieve more information about the class using System.Reflection and the corresponding methods. The Main() method of the example mentioned above can be modified as shown below to use few such methods of System.Reflection:

public static void Main() {
Type typeOfClass= typeof(sampleClass);
Console.WriteLine(“Basic Details about sampleClass:”);
Console.WriteLine(“Is it a class? “ + typeOfClass.IsClass);
Console.WriteLine(“Is it abstract? “ + typeOfClass.IsAbstract);
Console.WriteLine(“ “);
MethodInfo[] methodsInClass = typeOfClass.GetMethods();
Console.WriteLine(“Methods of sampleClass are:”);
foreach (MethodInfo method in methodsInClass) {
Console.WriteLine(method.ToString());
}
Console.WriteLine(“ “);
Console.WriteLine(“Members (including methods) of sampleClass are:”);
MemberInfo[] membersOfClass = typeOfClass.GetMembers();
foreach (MemberInfo member in membersOfClass) {
Console.WriteLine(member.ToString());
}
}

Output of this code will be:
Basic Details about sampleClass:
Is it a class? true
Is it abstract? false

Methods of sampleClass are:
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void setData(int, double)
Void displayData()
Void Main()
System.Type GetType()

Members (including methods) of sampleClass are:
Int32 intVar
Double doubleVar
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void setData(int, double)
Void displayData()
Void Main()
System.Type GetType()
Void .ctor()

So far, you have determined the type of a pre-determined type be it a value type or a class. What if you have to determine the type of an expression during runtime? You can do it using the GetType() method shown in the above output. Here is an example:

class sampleClass {
public static void Main() {
Console.WriteLine(“Type of the expression 10*12.5/7 is ” +
(10*12.5/7).GetType());
}
}

Output of this code will be:
Type of the expression 10*12.5/7 is System.Double

Not very specific to run time expressions, you can use GetType() method as an alternative to typeof operator as well. Consider the following piece of code:

sampleClass obj = new sampleClass();
Type typeOfClass = obj.GetType();
The above mentioned code is equivalent to:
Type typeOfClass = typeof(sampleClass);

Irrespective of which approach you use, you can apply any of the earlier discussed methods of System.Reflection to fetch additional information about the type variable.

| How does Yield Keyword of C# Use Lazy Evaluationm: Reference Types |How to Create Iterator to Process Generic List in C# | How to Use “add” and “remove” Contextual Keywords of C# | How to Use ForEach with Arrays in C# |Illustration of Operator Keywords (as, is) with Examples in C# | Illustration of Operator Keywords (new, sizeof) with Examples in C# |Illustration of Operator Keyword stackalloc with Examples in C# |Illustration of Operator Keywords (true, false) with Examples in C#|Illustration of Operator Keyword typeof with Examples in C# |List of Contextual Keywords in C#|


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.