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.

375 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 →

    7 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...
      • Panos TheofanopoulosPanos Theofanopoulos commented  ·   ·  Flag as inappropriate

        +1 If you allow this only when the extended type is the same as the enclosing type, ie

        class MyString {
        public static bool IsEmpty(this MyString me) {
        return me == null || me.Length==0;
        }

        That way instead of typing

        if ( a != null && a.Length > 0 )

        you can

        if ( a.IsEmpty() )

        +2 if interfaces are also supported... ie

        public interface ICharSequence {
        int Length{get;}
        public static bool IsEmpty(this ICharSequence me) {
        return me == null || me.Length==0;
        }
        }

        +3 If generic constraints are allowed, ie

        public class List<T> {
        public static int AddUniqueValue(this List<T> me, T value) where T : IEquatable<T> {

        and the AddUniqueValue method will only "appear" as instance method to the types that are IEquatable

        Note that you can accomplish all the above using another static class, but with that syntax you can have one class less and access to the private members
        Also note that, you already have similar behavior with operator overriding (declare a static method and invoke it as instance)

      • Shiv KumarShiv Kumar commented  ·   ·  Flag as inappropriate

        -1
        First off this is a bad idea.
        Secondly, extension methods are really compiler magic and don't really exist on the class that you're extending. Your suggestion will modify the class.

      • Wouter VosWouter Vos commented  ·   ·  Flag as inappropriate

        -1

        Seems to me extension methods are quite a change and can be unexpected by other programmers. Allowing any class to extend any other class goes in against the separation of responsibilities.

        Instead, define a separate class for all extension methods on a specific class, ClassNameExtensions. Yes this will lead to more codefiles, but at least it's clear where all extension methods are located.

      • Nobody RealNobody Real commented  ·   ·  Flag as inappropriate

        I doubt anything like this will happen, since extension methods are only possible because they are static. To put this another way, extension methods are just syntactic sugar over a call like this: myObject.MyExtension() is really MyExtension(myObject). In other words, extension methods don't *REALLY* add methods to objects, it just pretends to do. They would essentially have to change the basic nature of the object model, making all objects dynamic in order to do this, and I don't see that happening.. and if it did, we probably wouldn't like it.

      Feedback and Knowledge Base