c# - Extension method is not working,If method in nonstatic class? -


just read extension methods.i created static method inside static class working fine.

    static class extensionmethods     {         public static string splitfirstname(this string strname)         {             return strname.split(" ".tochararray())[0];         }      } 

but if create static method inside nonstatic class not working.

    class nonstaticcls     {       public static string splitfirstname(this string strname)       {         return strname.split(" ".tochararray())[0];       }     } 

please tell why not working in nonstatic class.

please tell why not working in nonstatic class.

because that's how extension methods specified. must declared in non-nested, non-generic class.

from section 10.6.9 of c# 5 specification:

when first parameter of method includes modifier, method said extension method. extension methods can declared in non-generic, non-nested static classes. first parameter of extension method can have no modifiers other this, , parameter type cannot pointer type.

why want declare in non-static class? trying achieve can't achieved equally using static class? (i can just imagine possibilities, they're not things i've ever wanted in extension methods myself...)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -