typeof generic and casted type [duplicate]
This question already has an answer here:
Type Checking: typeof, GetType, or is?
14 answers
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?
c# generics
marked as duplicate by Jeroen Vannevel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 22 at 21:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Type Checking: typeof, GetType, or is?
14 answers
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?
c# generics
marked as duplicate by Jeroen Vannevel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 22 at 21:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
typeof takes
a type name (which you specify at compile time),GetType
gets the runtime type of an instance.
– João Paulo Amorim
Feb 22 at 12:47
2
Also, if you find yourself doing any kind of type test inside a generic, ask yourself whether you've picked the right tool for the job. Because it may mean that you fail to work properly at runtime something that you "promised" at compile time you could do (by saying you could work for any type, subject to any generic type constraints on that type parameter)
– Damien_The_Unbeliever
Feb 22 at 12:49
add a comment |
This question already has an answer here:
Type Checking: typeof, GetType, or is?
14 answers
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?
c# generics
This question already has an answer here:
Type Checking: typeof, GetType, or is?
14 answers
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?
This question already has an answer here:
Type Checking: typeof, GetType, or is?
14 answers
c# generics
c# generics
edited Feb 22 at 12:59
Uwe Keim
27.6k32132212
27.6k32132212
asked Feb 22 at 12:44
user3450929user3450929
1305
1305
marked as duplicate by Jeroen Vannevel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 22 at 21:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jeroen Vannevel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 22 at 21:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
typeof takes
a type name (which you specify at compile time),GetType
gets the runtime type of an instance.
– João Paulo Amorim
Feb 22 at 12:47
2
Also, if you find yourself doing any kind of type test inside a generic, ask yourself whether you've picked the right tool for the job. Because it may mean that you fail to work properly at runtime something that you "promised" at compile time you could do (by saying you could work for any type, subject to any generic type constraints on that type parameter)
– Damien_The_Unbeliever
Feb 22 at 12:49
add a comment |
3
typeof takes
a type name (which you specify at compile time),GetType
gets the runtime type of an instance.
– João Paulo Amorim
Feb 22 at 12:47
2
Also, if you find yourself doing any kind of type test inside a generic, ask yourself whether you've picked the right tool for the job. Because it may mean that you fail to work properly at runtime something that you "promised" at compile time you could do (by saying you could work for any type, subject to any generic type constraints on that type parameter)
– Damien_The_Unbeliever
Feb 22 at 12:49
3
3
typeof takes
a type name (which you specify at compile time), GetType
gets the runtime type of an instance.– João Paulo Amorim
Feb 22 at 12:47
typeof takes
a type name (which you specify at compile time), GetType
gets the runtime type of an instance.– João Paulo Amorim
Feb 22 at 12:47
2
2
Also, if you find yourself doing any kind of type test inside a generic, ask yourself whether you've picked the right tool for the job. Because it may mean that you fail to work properly at runtime something that you "promised" at compile time you could do (by saying you could work for any type, subject to any generic type constraints on that type parameter)
– Damien_The_Unbeliever
Feb 22 at 12:49
Also, if you find yourself doing any kind of type test inside a generic, ask yourself whether you've picked the right tool for the job. Because it may mean that you fail to work properly at runtime something that you "promised" at compile time you could do (by saying you could work for any type, subject to any generic type constraints on that type parameter)
– Damien_The_Unbeliever
Feb 22 at 12:49
add a comment |
5 Answers
5
active
oldest
votes
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#?
AlthoughtypeOf
will get resolved before the first time a function is executed with a particularT
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every typeT
with which a function could be invoked, and thus impossible to determine whatT
would be prior to program execution.
– supercat
Feb 22 at 19:08
add a comment |
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.
add a comment |
The call to GetType gets resolved at runtime, while typeof is resolved at compile time.
That is why it is giving different results.
you can check here - When and where to use GetType() or typeof()?
add a comment |
This Tells me Typeof gives you compile time type whereas GetType gives you Exact Run time type.
add a comment |
A lot is made clear when you leave out the type interference:
GenericMethod(1)
is actually GenericMethod<int>(1)
.
GenericMethod((object) 1)
is inferred as GenericMethod<object>((object) 1)
.
When you ask typeof(T)
, it returns the T
you specified in the method call. You could also do GenericMethod<object>("a")
, which will return object
on typeof(T)
.
GetType
returns the actual runtime type of the instance provided.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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#?
AlthoughtypeOf
will get resolved before the first time a function is executed with a particularT
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every typeT
with which a function could be invoked, and thus impossible to determine whatT
would be prior to program execution.
– supercat
Feb 22 at 19:08
add a comment |
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#?
AlthoughtypeOf
will get resolved before the first time a function is executed with a particularT
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every typeT
with which a function could be invoked, and thus impossible to determine whatT
would be prior to program execution.
– supercat
Feb 22 at 19:08
add a comment |
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#?
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#?
edited Feb 22 at 13:22
answered Feb 22 at 12:47
HeinziHeinzi
123k38272409
123k38272409
AlthoughtypeOf
will get resolved before the first time a function is executed with a particularT
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every typeT
with which a function could be invoked, and thus impossible to determine whatT
would be prior to program execution.
– supercat
Feb 22 at 19:08
add a comment |
AlthoughtypeOf
will get resolved before the first time a function is executed with a particularT
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every typeT
with which a function could be invoked, and thus impossible to determine whatT
would be prior to program execution.
– supercat
Feb 22 at 19:08
Although
typeOf
will get resolved before the first time a function is executed with a particular T
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every type T
with which a function could be invoked, and thus impossible to determine what T
would be prior to program execution.– supercat
Feb 22 at 19:08
Although
typeOf
will get resolved before the first time a function is executed with a particular T
, it may occur after the program has started execution. Unlike C++, C# makes it possible for a program to create an unbounded number of different types based upon input, even without using Reflection. In such cases, it may be impossible to produce a list of every type T
with which a function could be invoked, and thus impossible to determine what T
would be prior to program execution.– supercat
Feb 22 at 19:08
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Feb 22 at 12:49
Henk HoltermanHenk Holterman
210k22232405
210k22232405
add a comment |
add a comment |
The call to GetType gets resolved at runtime, while typeof is resolved at compile time.
That is why it is giving different results.
you can check here - When and where to use GetType() or typeof()?
add a comment |
The call to GetType gets resolved at runtime, while typeof is resolved at compile time.
That is why it is giving different results.
you can check here - When and where to use GetType() or typeof()?
add a comment |
The call to GetType gets resolved at runtime, while typeof is resolved at compile time.
That is why it is giving different results.
you can check here - When and where to use GetType() or typeof()?
The call to GetType gets resolved at runtime, while typeof is resolved at compile time.
That is why it is giving different results.
you can check here - When and where to use GetType() or typeof()?
answered Feb 22 at 13:02
Deepankshee JainDeepankshee Jain
364
364
add a comment |
add a comment |
This Tells me Typeof gives you compile time type whereas GetType gives you Exact Run time type.
add a comment |
This Tells me Typeof gives you compile time type whereas GetType gives you Exact Run time type.
add a comment |
This Tells me Typeof gives you compile time type whereas GetType gives you Exact Run time type.
This Tells me Typeof gives you compile time type whereas GetType gives you Exact Run time type.
answered Feb 22 at 12:50
Gagan DeepGagan Deep
76616
76616
add a comment |
add a comment |
A lot is made clear when you leave out the type interference:
GenericMethod(1)
is actually GenericMethod<int>(1)
.
GenericMethod((object) 1)
is inferred as GenericMethod<object>((object) 1)
.
When you ask typeof(T)
, it returns the T
you specified in the method call. You could also do GenericMethod<object>("a")
, which will return object
on typeof(T)
.
GetType
returns the actual runtime type of the instance provided.
add a comment |
A lot is made clear when you leave out the type interference:
GenericMethod(1)
is actually GenericMethod<int>(1)
.
GenericMethod((object) 1)
is inferred as GenericMethod<object>((object) 1)
.
When you ask typeof(T)
, it returns the T
you specified in the method call. You could also do GenericMethod<object>("a")
, which will return object
on typeof(T)
.
GetType
returns the actual runtime type of the instance provided.
add a comment |
A lot is made clear when you leave out the type interference:
GenericMethod(1)
is actually GenericMethod<int>(1)
.
GenericMethod((object) 1)
is inferred as GenericMethod<object>((object) 1)
.
When you ask typeof(T)
, it returns the T
you specified in the method call. You could also do GenericMethod<object>("a")
, which will return object
on typeof(T)
.
GetType
returns the actual runtime type of the instance provided.
A lot is made clear when you leave out the type interference:
GenericMethod(1)
is actually GenericMethod<int>(1)
.
GenericMethod((object) 1)
is inferred as GenericMethod<object>((object) 1)
.
When you ask typeof(T)
, it returns the T
you specified in the method call. You could also do GenericMethod<object>("a")
, which will return object
on typeof(T)
.
GetType
returns the actual runtime type of the instance provided.
answered Feb 22 at 19:13
Patrick HofmanPatrick Hofman
127k18174232
127k18174232
add a comment |
add a comment |
3
typeof takes
a type name (which you specify at compile time),GetType
gets the runtime type of an instance.– João Paulo Amorim
Feb 22 at 12:47
2
Also, if you find yourself doing any kind of type test inside a generic, ask yourself whether you've picked the right tool for the job. Because it may mean that you fail to work properly at runtime something that you "promised" at compile time you could do (by saying you could work for any type, subject to any generic type constraints on that type parameter)
– Damien_The_Unbeliever
Feb 22 at 12:49