What are Extension Methods?

Technology CommunityCategory: LINQWhat are Extension Methods?
VietMX Staff asked 3 years ago

Extension methods are static functions of a static class. These methods can be invoked just like instance method syntax. These methods are useful when we can not want to modify the class. Consider:

public static class StringMethods
{
    public static bool IsStartWithLetterM(this string s)
    {
        return s.StartsWith("m");
    }
}
class Program
{
    static void Main(string[] args)
    {
        string value = "malslfds";
        Console.WriteLine(value.IsStartWithLetterM()); //print true;
 
        Console.ReadLine();
    }
}