Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I am creating one extension method So ,the class is static and the method also static !!
question is that ,How the CLR binding the extension call to the extension method, without the class name??
(Normally if the class is static and method is static we can call by using the class name..Here we are not specifying the class name..)

C#
public static class Exte
   {
       public static string GetFirstThreeCh(this string str)
       {
           if (str.Length < 3)
           {
               return str;
           }
           else
           {
               return str.Substring(0, 3);
           }
       }
   }


C#
static void Main(string[] args)
     {
         String str = "my new String";
         
         str = str.GetFirstThreeCh();
     }
Posted
Comments
Sergey Alexandrovich Kryukov 26-Apr-12 12:22pm    
Reason for my vote of 4
Interesting question, good for general understanding of general things in depth, but a bit naive.
--SA

There is nothing "internal" about it. The extension methods work "on top of SLR", they are just the syntactic sugar:
http://en.wikipedia.org/wiki/Syntactic_sugar[^].

The code of using of an extension method is strictly equivalent to the code of using a static method of extension class directly:

C#
string sample = //...
sample = str.GetFirstThreeCh();

//same as if you called:
sample = Exte.GetFirstThreeCh(str);


[EDIT]

This syntactic feature mimics the syntax of instance method with a static method.

For better understanding, see also my explanation on how instance and static methods work, in my past answer:
What makes static methods accessible?[^].

[EDIT]

By the way, in your sample method, you should also check up the argument for null, and, say, also return str, otherwise your method with throw exception at str.Length. In this case, I would consider this as a bug.

—SA
 
Share this answer
 
v6
Comments
Abhinav S 26-Apr-12 12:33pm    
Correct. 5.
Sergey Alexandrovich Kryukov 26-Apr-12 12:48pm    
Thank you, Abhinav.
--SA
VJ Reddy 26-Apr-12 13:05pm    
Good answer. +5
Sergey Alexandrovich Kryukov 26-Apr-12 13:13pm    
Thank you, VJ.
--SA
samu4u 27-Apr-12 2:55am    
5
Please see here for the extension method[^]definition and etc.

Now do bit of interesting work, in this research I use ILDasm(C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\ildasm.exe but this is for my installed VS) tool which comes as part of the VS installation. I use the same C# code as OP provided except change the class name Exte to ExtensionMethodClass.

I build the program and grab the exe of the program in this case it is ConsoleApplication24.exe (as I put the code in the console app which is called ConsoleApplication24.) into the ILDasm program and ILDasm generate following IL code for the ExtensionMethodClass class in here we define the extension method.

C#
.class public abstract auto ansi sealed beforefieldinit ExtensionMethodClass
    extends [mscorlib]System.Object
{
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
    .method public hidebysig static string GetFirstThreeCh(string str) cil managed
    {
        .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
        .maxstack 3
        .locals init (
            [0] string str2,
            [1] bool flag)
        //removed all the code for clearity
    }

}


If we look into the IL code above, we can see,

  • ExtensionMethodClass class has been define as abstract and sealed.
  • Inside this class GetFirstThreeCh method has been compiled as static public method which has string type parameter. NOTE that in here there is no this which we define in the C# code in the GetFirstThreeCh method.
  • So it is clear more this IL code that extension method has been define as same as static method.

Let’s see the usage of this GetFirstThreeCh extension method from the Main method which will show us the calling convention of the extension method in underneath. I extracted the following IL code from the program class of the ConsoleApplication24.exe program using the ILdasm,

C#
.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] string str)
    L_0000: nop 
    L_0001: ldstr "my new String"
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: call string ConsoleApplication24.ExtensionMethodClass::GetFirstThreeCh(string)
    L_000d: stloc.0 
    L_000e: ret 
}


From the above IL code in L_0008 label we can see that GetFirstThreeCh method has been call as same as CLR call static method.

To test this I created a small static class with a static method as below,

C#
namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
              TestStaticMethod.TestMethod();
        }
    }    

    public class TestStaticMethod 
    {
        public static void TestMethod() 
        {
        }
    }
}

After building this new program I decompiled using ILDasm program to generate the IL code which is as below,

C#
.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 8
    L_0000: nop 
    L_0001: call void ConsoleApplication24.TestStaticMethod::TestMethod()
    L_0006: nop 
    L_0007: ret 
}


So we can see that how does CLR handle the extension method in behind the scene.

Hope it explain the details :)
 
Share this answer
 
v2
Comments
samu4u 27-Apr-12 2:55am    
5
Mohammad A Rahman 27-Apr-12 3:00am    
Thank you :)
(__Aaron__) 27-Apr-12 5:22am    
Good explanation.
Mohammad A Rahman 27-Apr-12 5:26am    
Thank you :)
VJ Reddy 27-Apr-12 5:58am    
Nice explanation. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900