Some useful functions:
1. To Check For Null values:
public static bool CheckForNull(this object obj)
{
if(obj == null)
{
return false;
}
else
{
return true;
}
}
use this function for any object like:
public void SomeFunction()
{
If(SomeObj.CheckForNull()) //Check if SomeObje is null or not
{
//Do Something
.....
.....
}
}
2> To Cast To Integer Types:
public static Int16 ToShort(this object obj)
{
Int16 num = 0;
Int16.TryParse(obj.ToString(), out num);
return num;
}
public static Int64 ToLong(this object obj)
{
Int64 num = 0;
Int64.TryParse(obj.ToString(), out num);
return num;
}
use this function for any object like:
public void SomeFunction()
{
string strNum="2344";
int i= strNum.ToShort();
long j = strNum.ToLong();
}
(Note: All above mentioned Extension Methods should be defined in a static class.)