I suggest you ...

allow extension methods to be defined in instance classes

Currently, extension methods can only be defined in a static class. It would be very helpful if an extension method could also be defined in an instance class, so that this was possible:

public class Foo
{
public void DoBar()
{
new Qux().GetBaz("corge").DoGrault();
}

private static Baz GetBaz(this IQux qux, string s)
{
// Get Baz from qux here...
}
}

This simply enables a more fluent syntax for private helper methods. This can improve readability of the code compared to a more procedural-looking use of normal static helper methods.

The current workaround is to define a new internal static class, but that opens those helper methods to be used from other classes in the assembly, and sometimes such private helper methods aren't ready for that (they may not properly protect their invariants).

An alternative to the above code would be to allow extension methods to be defined in (private) nested classes.

283 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Mark SeemannMark Seemann shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    4 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base