0

Just now i read about Extension methods.I created static method inside static class its working fine.

    static class ExtensionMethods
    {
        public static string splitFirstName(this string strName)
        {
            return strName.Split(" ".ToCharArray())[0];
        }
     }

But If i create static method inside Nonstatic class its not working.

    class NonStaticCls
    {
      public static string splitFirstName(this string strName)
      {
        return strName.Split(" ".ToCharArray())[0];
      }
    }

Please tell why its not working in nonstatic class.

Prabhakaran Parthipan
  • 4,193
  • 2
  • 18
  • 27

5 Answers5

6

Please tell why its not working in nonstatic class.

Because that's just how extension methods are specified. They must be declared in a non-nested, non-generic class.

From section 10.6.9 of the C# 5 specification:

When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

Why do you want to declare it in a non-static class? What are you trying to achieve which can't be achieved equally well using a static class? (I can just about imagine some possibilities, but they're not things I've ever wanted to do in extension methods myself...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

It's a requirement that the class be static. This is reasonable. If it wasn't a static class, you'd be able (potentially) to create instances of that class. But then it has extension methods in it, possibly for other, completely unrelated classes. That could be very confusing.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

That's by design behavior. Extension method can only be declared in static class.

How to: Implement and Call a Custom Extension Method (C# Programming Guide)

To define and call the extension method

  1. Define a static class to contain the extension method.
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
2

Looks like it's by design.

From MSDN;

The following list provides the main features of a static class:

  • Contains only static members.

  • Cannot be instantiated.

  • Is sealed.

  • Cannot contain Instance Constructors.

Check out this answer from Eric Lippert

Also from AskJonSkeet

It's dictated in the language specification, section 10.6.9 of the C# 4 spec:

When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

According to C# spec. you can read more here Extension Methods

Extension methods can only be declared in non-generic, non-nested static classes.

AS other pointed you, you need to include the static.

basically you will create a bunch of extension method as Utility Project or Helper Class

  1. Creating static class you need not to instantiated, and invoke the method directly. for Eg. Math.Pow()
  2. as per your question, Extension method work only when it's in Static class. and use of proper namespace.
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83