I am trying t get this working but somehow its going out of my hand... I want to be able to check null or empty to whatever type i assigned.
EX:
int i =0;
string mystring = "";
var reult = CheckNullOrEmpty(10) // passing int
var result 1 = CheckNullOrEmpty(mystring) // passing string
public bool CheckNullOrEmpty<T>(T value)
{
// check for null or empty for strings
// check for null i.e. 0 for int
}
can someone help me with this.. I am trying to understand how generics works for this simple method.
IsNaN() Method in C# In C#, Double. IsNaN() is a Double struct method. This method is used to check whether the specified value is not a number (NaN).
Properties isEmpty() method in Java with Examples The isEmpty() method of Properties class is used to check if this Properties object is empty or not. Returns: This method returns a boolean value stating if this Properties object is empty or not.
Null refers to a value that is either empty or doesn't exist. null means no value. To make a variable null we must assign null value to it as by default in typescript unassigned values are termed undefined. We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.
Say, if a string is empty var name = "" then console. log(! name) returns true . this function will return true if val is empty, null, undefined, false, the number 0 or NaN.
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));
}
How to use:
class Stub { }
bool f1 = CheckNullOrEmpty(""); //true
bool f2 = CheckNullOrEmpty<string>(null); //true
bool f3 = CheckNullOrEmpty(0); //true
bool f4 = CheckNullOrEmpty<Stub>(null); //true
You can check against default(T);
public bool CheckNullOrEmpty<T>(T value)
{
return value == default(T);
}
For more informations: http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With