c# add method to class code example
Example 1: define extension methods c#
public static class MyExtensions
{
//use the this keyword before an argument
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Example 2: c# method info extension
private static readonly MethodInfo ExtensionMethod =
typeof(ObjectExtensions).GetMethod(nameof(ObjectExtensions.NewMethod));