c# string extension code example
Example 1: define extension methods c#
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
Example 2: c# string right extension
public static partial class Extensions
{
public static string Right(this string @this, int length)
{
return @this.Substring(@this.Length - length);
}
}
Example 3: c# method info extension
private static readonly MethodInfo ExtensionMethod =
typeof(ObjectExtensions).GetMethod(nameof(ObjectExtensions.NewMethod));