c sharp

C# Typeof

The C# includes an abstract class called Type to help you with this. We can’t just declare a variable of Type because it’s an abstract class. The C# language includes a type of operator that can help you determine the data type or class of a variable. This operator provides the equivalent C# type if you pass a C# data type as the operand such as char, int, float, and so on. The typeof operator in C# will be demonstrated in this article. The typeof operator, in reality, returns the operand’s Type name. The typeof operator, in reality, returns the operand’s Type name.

What is the typeof operator keyword in C# in Ubuntu 20.04?

The typeof keyword is an operator that is used to get a type at compile time. Alternatively, the typeof operator is used to retrieve a type’s System.type object. We can acquire the type’s name and namespace name by using the typeof () operator. As it only works with types that are known at compile time. The typeof () operator doesn’t apply to variables or instances.

Syntax of the typeof operator keyword in C# in Ubuntu 20.04

The type of operator’s general syntax is as follows:

# System.Type type = typeof (int);

Here “System.Type ” is an abstract class that enables the use of the typeof operator. The variable is declared with the type abstract class as “type”. The typeof operator is then used with a data type integer parameter. We can pass the data type of our choice in the typeof operator and Returns an object’s Type without requiring it to be instantiated. It’s frequently used as a parameter, variable, or field. The typeof operator in C# is used to obtain the Type pointer as part of an expression.

Key Features of the typeof operator keyword in C# in Ubuntu 20.04

  1. A type of parameter or the name of the type is always the operand of the typeof operator. There is no variable in it.
  2. It is forbidden to overload the typeof operator in the C# program.
  3. On open generic types, the typeof operator is acceptable.
  4. Both unbounded and bounded types can be utilized with the typeof operator.

How to use the typeof operator keyword in C# in Ubuntu 20.04

Take a look at the following examples to get a better understanding of how to use the typeof operator keyword in C#:

Example 1: Using the typeof operator keyword in C# in Ubuntu 20.04

The following C# program is to demonstrate the typeof operator. Let’s see how it will work in C# and its the outcomes.

The above code snippet import the system library “Using System” which will help us to access the C# classes and functions where required. By using the system library, we have created a class “MyProgram” in the C# program. In the block of the Specified class, we have invoked a “Type” as a variable here with the static keyword. The static keyword specifies that a member of a class has only one instance. The variable is defined with the name “Val” and the variable stores the typeof operator keyword there.

In the typeof operator, we have passed the type “double” as a parameter. Then, we have the main function of the given C# class which we have called the C# writeLine method. The writeLine method first displays the type of the variable “val”. After this, we have the typeof operator used in the writeLine method which displays the type of class Array, type of integer, char, and value type of array reference passed in the writeLine method.

The Different types for the different value types are shown in the image as follows:

Example 2: Using the typeof operator and GetType Operator keyword in C# in Ubuntu 20.04

We use the typeof operator to acquire the System.type object. It’s frequently used as a parameter, variable, or field. It is used to conduct a compile-time lookup, (i.e. get the Type object for a symbol that represents a Class name.) GetType, on the other hand, is a virtual method on Object that allows you to retrieve the current instance’s exact runtime type given a class instance. The runtime type of an object in memory is the type of object during runtime. Consequently, it is a run-time concept. The object type is returned by the GetType () function.

The C# class is defined as “program2” in the above code snippet. In the main function of the given class, we have declared a variable “s” of type string and initialized it with the string value. The Type class is used for the operator typeof and passes the type of “s” to it. Then, the getType method is called for the “s” variable. The writeLine method is used here to check the equality among the typeof field and the getType field. Then we have declared a type object for “obj” and assigned it a value. Using the typeof operator and getType method for the object type. As the GetType method is used, checking for equality in the writeLine method will yield a False to obtain a type that can be used during run-time.

The Boolean values are shown on the console screen below.

Example 3: Using the typeof operator keyword for String type in C# in Ubuntu 20.04

In the following program, we have a typeof operator keyword in which we have a string type parameter.

The class “program3” is defined in the above program. The class has the main function and in the block of the main function we have three variables of type string as “s1”, “s2”, and “s3”. The string variable “s1” is assigned a string of words. The variable “s2” has a string value of number and the third variable “s3” is storing an empty space string.  The type string of these specified variables is passed in the typeof operator as an operand/ type parameter. The typeof operator is called by the class “Type” with object declarations as “t1”, “t2”, and “t3”. The writeLine method will return the values of the string type variables and the type of the values in the variables.

The above program gives the outcome of the typed string shown in the image below.

Example 4: Using the typeof operator keyword for Integer type in C# in Ubuntu 20.04

We are taking integers as type parameters in the typeof operator in the following C# program.

The class of C# program is defined with the name “Program6”. In the class, we have the main function block in which we have created an object of array type reference and named “number”. We have several types int and several types long in the array of object “number”. The foreach loop is used here which creates the variable by using the “var” keyword and defines that variable as “num”. With the use of the “in” keyword, we have assigned the array elements to the variable “num”. In the foreach body, we have called the class “Type” for creating the “Type” class instance as “t1”. The object “t1” has a GetType method which will get the type of the “num”. Then, we have an if-else condition in which the “Equals” function is used to check if the type is matched or not. The Equals function is taking the typeof operator and the type “int” is passed as an operand for the typeof operator. The else statement will execute if the type int is not matched with the specified value of int.

The above program execution gives the following outcomes.

Conclusion

The article has briefly explained the typeof operator keyword in the C# program. We have also differentiated between the typeof operator and GetType method through an example program in Ubuntu 20.04. The syntax of the typeof operator used in the c# program is also demonstrated. Then, we have been clear about how to utilize the typeof operator to retrieve a Type instance representing a certain type through different examples. The article will give you good practice on the typeof operator.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.