Thursday, September 17, 2020

Extension Methods

  • Extension methods allow us to add new methods to an existing class without changing its source code or creating a new class that inherits from it. 
As an example , suppose we need a shorten version of a very long post. In that case, there is no instance method in String class to get it done. Also we can not inherit the String class as it is sealed class and can not change or modify. In that situation, we can use extension methods to add new methods to an existing class.  
  • Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on. The parameter is preceded by the this modifier.
  • namespace ExtentionMethods
    {
        public static class StringExtention
        {
            //numofwords is 5 means we need first 5 words of the str
            public static string Shorten(this string str, int numofwords)
            {
                if (numofwords == 0)
                    return " ";
    
                var words = str.Split(' ');
    
                if (words.Length <= numofwords)
                    return str;
    
                return string.Join(" ", words.Take(numofwords));
            }
        }
    }
    
    
  • Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. The "Shorten" extension method can be brought into scope with this "using ExtensionMethods;".
  • using ExtentionMethods;
    using System;
    
    namespace Example_ExtentionMethods
    {
        class Program
        {
            static void Main(string[] args)
            {
                string post = "This is supposed to be a very long post...";
                var shortenpost = post.Shorten(5);
                Console.WriteLine(shortenpost);
                Console.Read();
            }
        }
    }
    
    
  • At compile time, extension methods always have lower priority than instance methods defined in the type itself. In other words, if String class has an instance method "Shorten(int count)" and we have an extension method with the same signature for System namespace, the compiler will always bind to the instance method. In that case, our method will never be called.  

  • For those occasions when the original source isn't under our control, when a derived object is inappropriate or impossible, or when the functionality shouldn't be exposed beyond its applicable scope, Extension methods are an excellent choice.

  • When implement extension methods for a given type, should remember below points, 
    • An extension method will never be called if it has the same signature as a method defined in the type.
    • Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they'll all be brought into scope by the using Extensions; directive.
References : udemy.com , docs.microsoft.com

No comments:

Post a Comment