What\'s the exact difference between the two?
// When calling this method with GetByType()
public bool GetByType() {
// this ret
You should use is AClass
on instances and not to compare types:
var myInstance = new AClass();
var isit = myInstance is AClass; //true
is
works also with base-classes and interfaces:
MemoryStream stream = new MemoryStream();
bool isStream = stream is Stream; //true
bool isIDispo = stream is IDisposable; //true
typeof(T)
returns a Type
instance. and the Type
is never equal to AClass
var t1 = typeof(AClass)); // t1 is a "Type" object
var t2 = new AClass(); // t2 is a "AClass" object
t2 is AClass; // true
t1 is AClass; // false, because of t1 is a "Type" instance, not a "AClass" instance
typeof
returns a Type
object describing T
which is not of type AClass
hence the is
returns false.
The is
keyword checks if an object is of a certain type. typeof(T)
is of type Type
, and not of type AClass
.
Check the MSDN for the is keyword and the typeof keyword
your first statement is right
typeof(T) is AClass returns false because typeof(T) is Type and AClass does not inherit from Type