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).

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.

https://www.nuget.org/packages/Devcoons.Extensions/

Author

Write A Comment