In this post I want to cover some very basic and small extension methods which are very useful for any developer. All below mentioned extension method are not doing any complex operation. Rather they are just performing very simple task which you generally encounter a lots of times
1. IsNull
This is probably most commonly used expression. A lots of places in out code we check to null references. We usually add the obj == null or obj != null check to do this. Here is the IsNull extension method
1 2 3 4 5 6 |
public static bool IsNull(this object source) { return source == null; } |
Using the you can avoid the lazy null check. Example:
1 2 3 4 5 6 7 8 9 |
public void ProcessData(DataSet input) { if (!input.IsNull()) { // business logic here } } |
2. FormatString
We often use string.Format to format the string like
1 2 3 4 |
string message = string.Format("Welcome {0} (Last login: {1})", userName, lastLogin); |
The alternative way to do this is using below extension method
1 2 3 4 5 6 |
public static string FormatString(this string format, params object[] args) { return string.Format(format, args); } |
Now the above example can be re-written as
1 2 3 |
string message = "Welcome {0} (Last login: {1})".FormatString(userName, lastLogin); |
3. RaiseEvent
How you generally raise you events? I guess something like below
1 2 3 4 5 6 7 8 9 |
protected void OnMyEvent(EventArgs e) { if (MyEvent != null) { MyEvent(this, e); } } |
Here for each event you need to check the null reference before you raise it and also you create a new method so that you don’t have to add this check each place from where you are raising the event even if there is no special logic in the function. Here it the set of extension method to raise the events that matches the signature of EventHandler or EventHalder<TEventArgs>:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static void Raise(this EventHandler eventHandler, object sender, EventArgs e) { if (eventHandler != null) { eventHandler(sender, e); } } public static void Raise<T>(this EventHandler<T> eventHandler, object sender, T e) where T : EventArgs { if (eventHandler != null) { eventHandler(sender, e); } } |
Now this is how to raise the above event
1 2 3 |
MyEvent.Raise(this, e); |
4. Match
This extension method is for pattern matching in any string using Regex. This is how you generally use it
1 2 3 4 5 6 7 |
Regex regex = new Regex("[0-9]"); if (regex.IsMatch(myData)) { // do someting } |
And here is the extension method to do this simply
1 2 3 4 5 6 7 |
public static bool Match(this string value, string pattern) { Regex regex = new Regex(pattern); return regex.IsMatch(value); } |
After this look at the code below. It became much simpler than the traditional approach:
1 2 3 4 5 6 |
if (myData.Match("[0-9]")) { // do someting } |
5. ToInt
This method has three flavors, for Int16, Int32 and Int64. This can be easily used while doing string to int conversion. Here are the methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static long ToInt16(this string value) { Int16 result = 0; if (!string.IsNullOrEmpty(value)) Int16.TryParse(value, out result); return result; } public static long ToInt32(this string value) { Int32 result = 0; if (!string.IsNullOrEmpty(value)) Int32.TryParse(value, out result); return result; } public static long ToInt64(this string value) { Int64 result = 0; if (!string.IsNullOrEmpty(value)) Int64.TryParse(value, out result); return result; } |