Is there a support in C# 4.0 to do that in one line?
I did for objects:
if (ReferenceEquals(null, myDynamicVar))
so now I need to see if this a zero. how to do it and is there a statement that can do both?
Is there a support in C# 4.0 to do that in one line?
I did for objects:
if (ReferenceEquals(null, myDynamicVar))
so now I need to see if this a zero. how to do it and is there a statement that can do both?
if (ReferenceEquals(null, myDynamicVar) || Equals(0, myDynamicVar)) ...
public bool IsDefault<T>(T value)
{
if(value == null) return true;
return value.Equals(default(T));
}
int v = 5;
object o = null;
IsDefault(v); //False
IsDefault(0); //True
IsDefault(o); //True
IsDefault("ty"); //False
Sloppy:
if (ReferenceEquals(null, myDynamicVar) || myDynamicVar ==
(myDynamicVar.GetType().IsValueType ? Activator.CreateInstance(myDynamicVar.GetType()) : null)
{
//Code greatness
}
Cleaner:
public static bool IsDefault(dynamic input)
{
if (input == null)
{
return true;
}
else if (input.GetType().IsValueType)
{
return input == Activator.CreateInstance(input.GetType());
}
else
{
return false;
}
}