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?
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.
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.
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.
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.
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:
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With