Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call an extension method as a static method when using static import?

Background:

I had a static class, but the static methods weren't extension methods. I decided to refactor the methods into extension methods and didn't expect any code to break since extension methods can be called like static methods. However, code did break when static import was used for the static class holding the extension methods.

Example:

I have a static class with an extension method and a static method:

namespace UsingStaticExtensionTest.Extensions
{
    static class ExtensionClass
    {
        internal static void Test1(this Program pg)
        {
            System.Console.WriteLine("OK");
        }

        internal static void Test2(Program pg)
        {
            System.Console.WriteLine("OK");
        }

    }
}

When I use the following using directive, everything in the test program works fine:

using UsingStaticExtensionTest.Extensions;
namespace UsingStaticExtensionTest

    {
        class Program
        {
            static void Main(string[] args)
            {
                var p = new Program();
                ExtensionClass.Test1(p); // OK
                p.Test1(); // OK
                ExtensionClass.Test2(p); // OK
            }
        }
    }

But when I use the static import using directive to identify just the class with the extension method, I can't call the extension method as a static method:

using static UsingStaticExtensionTest.Extensions.ExtensionClass;
    class Program
    {
        static void Main(string[] args)
        {
            var p = new Program();
            //Test1(p); // Error: The name Test1 does not exist in the current context
            p.Test1(); // OK
            Test2(p); // OK **I can still call the static method**
        }
    }
}

Question: Why can't I call an extension method as a static method when using a static import?

like image 453
astidham2003 Avatar asked Aug 16 '16 21:08

astidham2003


People also ask

Can extension methods be static?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Why do extension methods need to be in a static class?

It is compulsion that the Extension method must be in a Static class only so that only one Instance is created. For example, if you place the following in ASP.Net page it will not work. Though error will not come, but you will not see the method available. The above will not work.

Can a static class be extended in C#?

If you are talking about using the extension method system to extend a static class then no, you cannot. Extension methods require an object (instance of a class) that will be passed in as the (this) parameter to the static method. Static classes cannot be extended in this way, because they are not instances.

Can extension methods extend non static classes?

Actually I'm answering the question of why extension methods cannot work with static classes. The answer is because extension methods are compiled into static methods that cannot recieve a reference to a static class.


1 Answers

Because of language design:

Using static makes extension methods declared in the specified type available for extension method lookup. However, the names of the extension methods are not imported into scope for unqualified reference in code.

using Directive

like image 149
Hamlet Hakobyan Avatar answered Oct 16 '22 08:10

Hamlet Hakobyan