Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and where to use GetType() or typeof()? [duplicate]

Why this works

if (mycontrol.GetType() == typeof(TextBox)) {}  

and this do not?

Type tp = typeof(mycontrol); 

But this works

Type tp = mycontrol.GetType(); 

I myself use is operator for checking type but my understanding fails when I use typeof() and GetType()

Where and when to use GetType() or typeof()?

like image 936
Nikhil Agrawal Avatar asked Jul 03 '12 13:07

Nikhil Agrawal


People also ask

What is the difference between GetType and typeof?

typeof keyword takes the Type itself as an argument and returns the underline Type of the argument whereas GetType() can only be invoked on the instance of the type.

What is the use of typeof ()?

typeof is an operator to obtain a type known at compile-time (or at least a generic type parameter). The operand of typeof is always the name of a type or type parameter - never an expression with a value (e.g. a variable). See the C# language specification for more details.

How do I use GetType?

The GetType method is inherited by all types that derive from Object. This means that, in addition to using your own language's comparison keyword, you can use the GetType method to determine the type of a particular object, as the following example shows.

What is GetType?

GetType Method is used to find the type of the current instance. This method returns the instances of the Type class that are used for consideration. Syntax: public Type GetType (); Return Value: This method return the exact runtime type of the current instance.


2 Answers

typeof is an operator to obtain a type known at compile-time (or at least a generic type parameter). The operand of typeof is always the name of a type or type parameter - never an expression with a value (e.g. a variable). See the C# language specification for more details.

GetType() is a method you call on individual objects, to get the execution-time type of the object.

Note that unless you only want exactly instances of TextBox (rather than instances of subclasses) you'd usually use:

if (myControl is TextBox) {     // Whatever } 

Or

TextBox tb = myControl as TextBox; if (tb != null) {     // Use tb } 
like image 120
Jon Skeet Avatar answered Sep 20 '22 00:09

Jon Skeet


typeof is applied to a name of a type or generic type parameter known at compile time (given as identifier, not as string). GetType is called on an object at runtime. In both cases the result is an object of the type System.Type containing meta-information on a type.

Example where compile-time and run-time types are equal

string s = "hello";  Type t1 = typeof(string); Type t2 = s.GetType(); 

t1 == t2 ==> true

Example where compile-time and run-time types are different

object obj = "hello";  Type t1 = typeof(object); // ==> object Type t2 = obj.GetType();  // ==> string! 

t1 == t2 ==> false

i.e., the compile time type (static type) of the variable obj is not the same as the runtime type of the object referenced by obj.


Testing types

If, however, you only want to know whether mycontrol is a TextBox then you can simply test

if (mycontrol is TextBox) 

Note that this is not completely equivalent to

if (mycontrol.GetType() == typeof(TextBox))     

because mycontrol could have a type that is derived from TextBox. In that case the first comparison yields true and the second false! The first and easier variant is OK in most cases, since a control derived from TextBox inherits everything that TextBox has, probably adds more to it and is therefore assignment compatible to TextBox.

public class MySpecializedTextBox : TextBox { }  MySpecializedTextBox specialized = new MySpecializedTextBox(); if (specialized is TextBox)       ==> true  if (specialized.GetType() == typeof(TextBox))        ==> false 

Casting

If you have the following test followed by a cast and T is nullable ...

if (obj is T) {     T x = (T)obj; // The casting tests, whether obj is T again!     ... } 

... you can change it to ...

T x = obj as T; if (x != null) {     ... } 

Testing whether a value is of a given type and casting (which involves this same test again) can both be time consuming for long inheritance chains. Using the as operator followed by a test for null is more performing.

Starting with C# 7.0 you can simplify the code by using pattern matching:

if (obj is T t) {     // t is a variable of type T having a non-null value.     ... } 

Btw.: this works for value types as well. Very handy for testing and unboxing. Note that you cannot test for nullable value types:

if (o is int? ni) ===> does NOT compile! 

This is because either the value is null or it is an int. This works for int? o as well as for object o = new Nullable<int>(x);:

if (o is int i) ===> OK! 

I like it, because it eliminates the need to access the Nullable<T>.Value property.

like image 28
Olivier Jacot-Descombes Avatar answered Sep 20 '22 00:09

Olivier Jacot-Descombes