Most of time we check a variable is Null or not. So below code snippet defines a generic way to check a variable is null. It will work with different variable type string, integer, custom object etc.
Here it defines a function CheckNullOrEmpty with parameter value of Type T. This function returns boolean value(True) if variable is null otherwise False.
class Common
{
public static bool CheckNullOrEmpty<T>(T value)
{
if (typeof(T) == typeof(string))
return string.IsNullOrEmpty(value as string);
return value == null || value.Equals(default(T));
}
public void TestFunc()
{
bool f1 = CheckNullOrEmpty(""); //true
bool f2 = CheckNullOrEmpty<string>(null); //true
bool f3 = CheckNullOrEmpty(0); //true
bool f4 = CheckNullOrEmpty<Stub>(null); //true
}
}