Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeof generic and casted type [duplicate]

Tags:

Let's say we have generic method:

public void GenericMethod<T>(T item) {     var typeOf = typeof(T);     var getType = item.GetType(); } 

And we are invoking it with the following parameters:

GenericMethod(1) GenericMethod((object) 1) 

The results are:

typeOf = System.Int32 getType = System.Int32 

and

typeOf = System.Object getType = System.Int32 

Can someone explain me why typeof integer casted to object returns System.Object, but .GetType() returns System.Int32?

like image 285
user3450929 Avatar asked Feb 22 '19 12:02

user3450929


People also ask

What are generics used for?

Generics enable the use of stronger type-checking, the elimination of casts, and the ability to develop generic algorithms. Without generics, many of the features that we use in Java today would not be possible.

Why use generics instead of object?

There are few reasons that you can consider Generics over Object type in Java: Generics is flexible and safe. At the same time, working with Object that requires type-casting is error-prone.

How do you find the type of generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

How do generics work in Java?

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.


2 Answers

typeof returns the static (compile-time) type of the generic parameter T.

GetType returns the dynamic (run-time) type of the value contained in variable item.


The difference is easier to see if you make your method non-generic. Let's assume that B is a subtype of A:

public void NonGenericMethod(A item) {     var typeOf = typeof(A);     var getType = item.GetType(); } 

In that case, calling NonGenericMethod(new B()) would yield

A B 

Recommended further reading:

  • Run-time type vs compile-time type in C#

Now, you might ask: Why did you use NonGenericMethod(A item) in your example instead of NonGenericMethod(B item)? That's a very good question! Consider the following (non-generic) example code:

public static void NonGenericMethod(A item) {     Console.WriteLine("Method A");     var typeOf = typeof(A);     var getType = item.GetType(); } public static void NonGenericMethod(B item) {     Console.WriteLine("Method B");     var typeOf = typeof(B);     var getType = item.GetType(); } 

What do you get when you call NonGenericMethod((A) new B()) (which is analogous to the argument (object) 1 in your example)?

Method A A B 

Why? Because overload resolution is done at compile-time, not at run-time. At compile-time, the type of the expression (A) new B() is A, just like the compile-time type of (object) 1 is object.

Recommended further reading:

  • When is the generic type resolved in c#?
like image 126
Heinzi Avatar answered Nov 03 '22 00:11

Heinzi


In GenericMethod((object) 1), T will be object. typeof reflects that.

But item.GetType(); is a virtual method and will execute at runtime on Int32.

like image 36
Henk Holterman Avatar answered Nov 02 '22 23:11

Henk Holterman