none
Why extension method always wrapped in static class RRS feed

  • Question

  • looking for few good reason why extension method always wrapped in static class. please explain with example.

    also mention what bad consequences would occur if it could be declare in regular class instead of static class with example ?

    thanks

    Tuesday, February 11, 2020 6:54 PM

Answers

  • This is how Microsoft designed them so that a developer can use an extension method without creating an instance of a specific class.

    See the documentation.

    If you are new to extension methods check these out.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, February 11, 2020 7:58 PM
    Moderator
  • looking for few good reason why extension method always wrapped in static class. please explain with example.

    A static class doesn't have to be instanced, and I view an extension method in a static class as an utility method not unlike making a static utility class that I need to make that have functions that can be called from any class without having to instance the utility class.

    You instance the class, and it slows the program down. 

    Think about it....

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Tuesday, February 11, 2020 8:03 PM
  • Hi Sudip_inn,

    Thank you for posting here.

    Generally speaking, extension methods are functional codes. It doesn't make much sense on its own, it must rely on certain types of objects, it's just a tool.

    Moreover, a class that contains extension methods will not usually have only one extension method.

    Think about it, if there is a class with 1000 extension methods, and we want to use one of them, if this class is an ordinary class, we must create this complex and huge class for this method, which will certainly affect efficiency to a certain extent and cause memory space waste.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 2:02 AM
  • To know more about extension methods in c# refer following article.

    https://www.tutlane.com/tutorial/csharp/csharp-extension-methods

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 11:40 AM
  • looking for few good reason why extension method always wrapped in static class. please explain with example.

    Let me try to explain it with an example.

    What you are trying to accomplish with the extension method is this:

    var myObject = new MyClass();
    var result = myObject.MyMethod();

    That is, you want to invoke MyMethod on an object, where MyMethod is not in MyClass. Of course, you can write MyMethod inside MyClass and this would work and it would not be in a static class. But then it would be a normal method, not an extension method. The idea of the extension method is that it is written outside the class to which it applies.

    Now, suppose that it was written as non-static. Then, in order to execute it, you would need to create an instance of the class that contains it. But given the way in which we invoke the method:

    myObject.MyMethod()

    , there is no opportunity to indicate the instance. So we cannot use this particular syntax to indicate the instance. of course you can instance the class and call the method in a different way: myInstance.MyMethod(myObject), but then it is no longer an extension method, we are once again using a plain ordinary method.

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 12:10 PM
    Moderator
  • A couple of reasons were mentioned when the concept was introduced and they all were wrapped around compile and runtime performance.

    Compile time - extension methods look just like instance methods so when you call a method on a member the compiler first checks for the member on the type (and its base types). If it doesn't find it then it has to start looking for extension methods. Looking at all available types and then looking at all the methods on each type would be a time consuming process during compilation. Therefore the compiler follows a narrowed down set of rules starting with only public/internal classes that are static. This eliminates a significant portion of the types it would otherwise have to look at.

    Runtime - an extension method call is converted to the static method equivalent by the compiler.

    public static class SomeExtensions
    {
       public static void Foo ( this string source ) { }
    }
    
    "Hello".Foo();
    
    //Rewritten as
    SomeExtensions.Foo("Hello");

    If it were a regular (non-static) class then the compiler would need to generate the following code instead.

    public class SomeExtensions
    {
       public void Foo ( this string source ) { }
    }
    
    "Hello".Foo();
    
    //Rewritten to
    new SomeExtensions().Foo("Hello");

    Creating an instance of a class just to call a method on it is wasteful so a static method makes more sense. One could argue that a static method could be put on an instance class and you'd be correct. Then you eliminate the need for the new object however what benefit do you gain from this? A static method can only access static members of a type so having any instance members on a class that has an extension method would not benefit the extension method at all. Other than for "consolidating" functionality it would gain you nothing. And because of the SRP it is generally a good idea to keep members of a type related it therefore makes sense that if you happen to need both instance and extension methods for a type then you are better off having an "instance" class with its related functionality separate from the extension type.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 2:58 PM
    Moderator

All replies

  • This is how Microsoft designed them so that a developer can use an extension method without creating an instance of a specific class.

    See the documentation.

    If you are new to extension methods check these out.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, February 11, 2020 7:58 PM
    Moderator
  • looking for few good reason why extension method always wrapped in static class. please explain with example.

    A static class doesn't have to be instanced, and I view an extension method in a static class as an utility method not unlike making a static utility class that I need to make that have functions that can be called from any class without having to instance the utility class.

    You instance the class, and it slows the program down. 

    Think about it....

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Tuesday, February 11, 2020 8:03 PM
  • Hi Sudip_inn,

    Thank you for posting here.

    Generally speaking, extension methods are functional codes. It doesn't make much sense on its own, it must rely on certain types of objects, it's just a tool.

    Moreover, a class that contains extension methods will not usually have only one extension method.

    Think about it, if there is a class with 1000 extension methods, and we want to use one of them, if this class is an ordinary class, we must create this complex and huge class for this method, which will certainly affect efficiency to a certain extent and cause memory space waste.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 2:02 AM
  • To know more about extension methods in c# refer following article.

    https://www.tutlane.com/tutorial/csharp/csharp-extension-methods

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 11:40 AM
  • looking for few good reason why extension method always wrapped in static class. please explain with example.

    Let me try to explain it with an example.

    What you are trying to accomplish with the extension method is this:

    var myObject = new MyClass();
    var result = myObject.MyMethod();

    That is, you want to invoke MyMethod on an object, where MyMethod is not in MyClass. Of course, you can write MyMethod inside MyClass and this would work and it would not be in a static class. But then it would be a normal method, not an extension method. The idea of the extension method is that it is written outside the class to which it applies.

    Now, suppose that it was written as non-static. Then, in order to execute it, you would need to create an instance of the class that contains it. But given the way in which we invoke the method:

    myObject.MyMethod()

    , there is no opportunity to indicate the instance. So we cannot use this particular syntax to indicate the instance. of course you can instance the class and call the method in a different way: myInstance.MyMethod(myObject), but then it is no longer an extension method, we are once again using a plain ordinary method.

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 12:10 PM
    Moderator
  • A couple of reasons were mentioned when the concept was introduced and they all were wrapped around compile and runtime performance.

    Compile time - extension methods look just like instance methods so when you call a method on a member the compiler first checks for the member on the type (and its base types). If it doesn't find it then it has to start looking for extension methods. Looking at all available types and then looking at all the methods on each type would be a time consuming process during compilation. Therefore the compiler follows a narrowed down set of rules starting with only public/internal classes that are static. This eliminates a significant portion of the types it would otherwise have to look at.

    Runtime - an extension method call is converted to the static method equivalent by the compiler.

    public static class SomeExtensions
    {
       public static void Foo ( this string source ) { }
    }
    
    "Hello".Foo();
    
    //Rewritten as
    SomeExtensions.Foo("Hello");

    If it were a regular (non-static) class then the compiler would need to generate the following code instead.

    public class SomeExtensions
    {
       public void Foo ( this string source ) { }
    }
    
    "Hello".Foo();
    
    //Rewritten to
    new SomeExtensions().Foo("Hello");

    Creating an instance of a class just to call a method on it is wasteful so a static method makes more sense. One could argue that a static method could be put on an instance class and you'd be correct. Then you eliminate the need for the new object however what benefit do you gain from this? A static method can only access static members of a type so having any instance members on a class that has an extension method would not benefit the extension method at all. Other than for "consolidating" functionality it would gain you nothing. And because of the SRP it is generally a good idea to keep members of a type related it therefore makes sense that if you happen to need both instance and extension methods for a type then you are better off having an "instance" class with its related functionality separate from the extension type.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Sudip_inn Friday, February 14, 2020 9:02 AM
    Wednesday, February 12, 2020 2:58 PM
    Moderator
  • Thanks Madam,

    when we call static class function then we first mention static class name and then mention static function name but in case of extension method we do not need to mention static class name. suppose i have 5 static class and every static class has few extension function then how .Net understand where the extension defined? means in which class extension method define?

    when we just write dot then all extension method name comes for that instance....how .net does it behind the scene.

    thanks

    Friday, February 14, 2020 2:55 PM
  • tell me.

    when we call static class function then we first mention static class name and then mention static function name but in case of extension method we do not need to mention static class name. suppose i have 5 static class and every static class has few extension function then how .Net understand where the extension defined? means in which class extension method define?

    when we just write dot then all extension method name comes for that instance....how .net does it behind the scene.

    thanks

    Friday, February 14, 2020 2:56 PM
  • tell me.

    when we call static class function then we first mention static class name and then mention static function name but in case of extension method we do not need to mention static class name. suppose i have 5 static class and every static class has few extension function then how .Net understand where the extension defined? means in which class extension method define?

    when we just write dot then all extension method name comes for that instance....how .net does it behind the scene.

    Generally speaking, extension methods are functional codes. It doesn't make much sense on its own, it must rely on certain types of objects, it's just a tool.

    Moreover, a class that contains extension methods will not usually have only one extension method.

    yes it relies on certain type but how it understand where the extension method defined....how IDE show the right extension method name for specific extension ?

    thanks

    Friday, February 14, 2020 2:58 PM
  • tell me.

    when we call static class function then we first mention static class name and then mention static function name but in case of extension method we do not need to mention static class name. suppose i have 5 static class and every static class has few extension function then how .Net understand where the extension defined? means in which class extension method define?

    when we just write dot then all extension method name comes for that instance....how .net does it behind the scene.

    thanks

    Friday, February 14, 2020 3:01 PM
  • tell me.

    when we call static class function then we first mention static class name and then mention static function name but in case of extension method we do not need to mention static class name. suppose i have 5 static class and every static class has few extension function then how .Net understand where the extension defined? means in which class extension method define?

    when we just write dot then all extension method name comes for that instance....how .net does it behind the scene.

    thanks

    Friday, February 14, 2020 3:03 PM
  • The rules for how extension methods are resolved is defined here. It doesn't follow the same rules as normal resolution but basically the compiler starts by looking for an instance method. If it doesn't find one then it looks for the first extension method that it can find that matches the signature. Since extension classes are static it can quickly find all static classes that have extension methods for the type on which the method is being called (because they are marked as such). Once it finds an extension class with a matching extension method and signature it knows everything it needs to generate a call to that static method. 

    If you happen to have multiple classes with the same extension method then whichever one it finds first it'll use. Example.

    public static class StringExtensions1
    {
       public static void Foo1 () { }
    }
    
    public static class StringExtensions2
    {
       public static void Foo2 () { }
    }
    
    public static class StringExtensions3
    {
       public static void Foo3 () { }
    }
    
    public static class StringExtensions4
    {
       public static void Foo3 () { }
    }
    
    "".Foo1(); //Only StringExtensions1 matches
    "".Foo2(); //Only StringExtensions2 matches
    "".Foo3(); //Either StringExtensions3 or StringExtensions4 matches so first one wins
    Note that namespaces play a part in this but the compiler doesn't look at every namespace to find extensions. The rules are a little bizarre and there are outstanding issues on name resolution. You can look at Github for Roslyn and see the list of issues/enhancements to resolution for extension methods if you're interested.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, February 14, 2020 3:40 PM
    Moderator