Possible Duplicate:
How do I compare a generic type to its default value?
I have a generic function that needs to test if the object that is passed into it is empty or not. But because its a generic type, the compiler doesnt know if a class or a struct is passed. Because of this I cant test for null I have to test if the type is empty.
public virtual void SetFocusedObject(T obj)
{
//since we dont know if T is a class or a struct test against default
T defaultT = default(T);
if(obj != defaultT)
{
//code
}
}
This does not work and its because the compiler doesnt know what T is to be able to compile the test
alternatively I tried the following as well
public virtual void SetFocusedObject(T obj)
{
//since we dont know if T is a class or a struct test against empty type
T defaultT = T.GetConstructor(T.EmptyTypes).Invoke(null);
if(obj != defaultT)
{
//code
}
}
And for the same exact reason, this does not work either. I was hoping that someone might suggest a method that will work.