I propose you to discover the methods of extensions that I use most often and which will certainly be able to serve you to gain efficiency in everyday life.
IsNull
1 2 3 4 5 6 7 8 9 10 |
static class MyExtensionMethods { // Check if an object is null public static bool IsNull(this object source) { return source == null; } } |
This extension can be used like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Program { static void Main(string[] args) { int? i; i = 10; i = null; if (i.IsNull()) { // treat the null case Console.WriteLine("i is null"); } Console.ReadLine(); } } |
FormatWith
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static class MyExtensionMethods { // Format a string of characters public static string FormatWith(this string format, params object[] args) { return string.Format(format, args); } // Check if an object is null public static bool IsNull(this object source) { return source == null; } } |
This extension can be used like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Program { static void Main(string[] args) { int a = 20, b = 50; var result = "This is the result: A={0}, B={1}".FormatWith(a, b); Console.WriteLine(result); Console.ReadLine(); } } |
ToInt / ToDouble
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 32 33 34 35 36 |
static class MyExtensionMethods { // Convert a string to int with default public static int ToInteger(this string value, int defaultvalue) { return (int)ToDouble(value, defaultvalue); } // Convert to int (default = 0) public static int ToInteger(this string value) { return ToInteger(value, 0); } //Convert to double public static double ToDouble(this string value) { return ToDouble(value, 0); } // Convert to duplicate with default public static double ToDouble(this string value, double defaultvalue) { double result; if (double.TryParse(value, out result)) { return result; } else return defaultvalue; } } |
You can use these extensions like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Program { static void Main(string[] args) { int value = "1233".ToInteger(); // call with a default value double dub = "3.14".ToDouble(1.0); Console.ReadLine(); } } |
In
1 2 3 4 5 6 7 8 9 10 11 |
static class MyExtensionMethods { // Check if an element is contained in a list public static bool In<T>(this T source, params T[] list) { if (null == source) throw new ArgumentNullException("source"); return list.Contains(source); } } |
This practical method can be used to replace this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
if (reallyLongIntegerVariableName == 1 || reallyLongIntegerVariableName == 6 || reallyLongIntegerVariableName == 9 || reallyLongIntegerVariableName == 11) { // do something... } if (reallyLongStringVariableName == "string1" || reallyLongStringVariableName == "string2" || reallyLongStringVariableName == "string3") { // do something... } if (reallyLongMethodParameterName == SomeEnum.Value1 || reallyLongMethodParameterName == SomeEnum.Value2 || reallyLongMethodParameterName == SomeEnum.Value3 || reallyLongMethodParameterName == SomeEnum.Value4) { // do something... } |
By this one (simpler and more readable):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
if (reallyLongIntegerVariableName.In(1, 6, 9, 11)) { // do something... } if (reallyLongStringVariableName.In("string1", "string2", "string3")) { // do something... } if (reallyLongMethodParameterName.In(SomeEnum.Value1, SomeEnum.Value2, SomeEnum.Value3, SomeEnum.Value4) { // do something... } |