Reputation: 28558
For a method
public void Foo<T>(T o)
{
var oType = o.GetType();
var tType = typeof(T);
}
Is there anything I should consider when choosing which one to use, either functional or non-functional?
I've created a fiddle to demonstrate it:
https://dotnetfiddle.net/Y4bAGf
Upvotes: 4
Views: 69
Reputation: 437734
Of course: you should consider whether you want the resulting type to match the static or runtime type of the argument.
This may be the same type in your typical use case, but that cannot be taken for granted. For example:
Foo<object>(string.Empty); // oType = string, tType = object
Moreover, in the general case you have to be careful to not call
GetType
on null
.
Upvotes: 4