2

Is it possible to create an extension method that takes no parameters? I am new to using extension methods and have only seen them using parameters.

From RB Whitaker's C# tutorial:

public static class StringExtensions
{
    private static Random random = new Random();

    public static string ToRandomCase(this string text)
    {
        string result = "";

        for (int index = 0; index < text.Length; index++)
        {
            if (random.Next(2) == 0)
            {
                result += text.Substring(index, 1).ToUpper();
            }
            else
            {
                result += text.Substring(index, 1).ToLower();
            }                
        }

        return result;
    }
}

And also from MSDN:

"Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier."

Both of these, and especially the code examples on each site (only one posted here), seem to indicate that extension methods must have at least one parameter, because that parameter is used to attach the this keyword to, allowing the method to register as an extension to the class or type which follows it. For example,

public static StringExtension(this String a) {/*stuff*/}

(If this is the case, then that also means that the first parameter in an extension method must accept an instance of the class it's extending, so I am probably wrong.)

  • 2
    Why would you want a parameterless extension method ? What would it extend then ? – Luc Morin Feb 24 '14 at 02:19
  • I was thinking it would operate like any method in a standard library that takes no parameters, such as Console.Read(). The question meant that you could define an extension method without parameters and (theoretically) call it like Console.MyExt(); but I see that it's not the case. –  Feb 24 '14 at 02:27
  • @Stopforgettingmyaccounts... Note that extension methods require an *instance*, but `Console` is a class, so you won't be able to write an extension method that can be used as a static method like `Console.MyExt()`. See [Can I add extension methods to an existing static class?](http://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class) – p.s.w.g Feb 24 '14 at 02:45
  • It would be rather pointless if the class was static. –  Feb 24 '14 at 02:57

1 Answers1

8

That's correct. Extension methods require at least one parameter, marked with the this modifier.

However, when invoking the extension method with a reference to the class you're extending, you wouldn't specify the parameter:

"foo".StringExtension();

Is equivalent to:

StringExtensions.StringExtension("foo");

Even if you don't actually use the parameter within your method, declaring it is necessary in order to use the syntax provided by extension methods.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Excellent, so when invoking the extension method, the first parameter is hidden? –  Feb 24 '14 at 02:29
  • 1
    @Stopforgettingmyaccounts... Correct. The compiler implicitly passes the object to the left of the `.` as the first parameter to the function. – p.s.w.g Feb 24 '14 at 02:31