In this article, I would like to mention benefits of using “Extension Methods” and provide a Nuget Package containing some of them. This feature started to be available in C # 3.0 compilers and continued in further versions (C# 4.0); it is very important for all developers especially if you would like to use the dynamism of the C# enhancements to take place in your class design.
How to Create a simple Extension Method?
Simply, create your own static method and put this keyword in front of the first parameter in this method (the type that will be extended).
1 2 3 4 5 6 7 8 |
public static class ClassExtensions { public static bool HasMethod(this object obj, string methodName) { var type = obj.GetType(); return type.GetMethod(methodName) != null; } } |
Below is a link to download my NuGet Package which contains several extension methods for the everyday problems.