Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any plans for "extension properties" in a future version of C#?

I've thought of this before and it came to mind again when reading this question.

Are there any plans for "extension properties" in a future version of C#?

It seems to me they might be pretty stright-forward to implement with a little more "compiler magic". For example, using get_ and set_ prefixes on extension method names would turn that method into an extension property:

    public class Foo
    {
        public string Text { get; set; }
    }
    public static class FooExtensions
    {
        public static string get_Name(this Foo foo)
        {
            return foo.Text;
        }
        public static void set_Name(this Foo foo, string value)
        {
            foo.Text = value; 
        }
    }

Are there any technical restrictions which would prevent this? Would this create too much stuff going on behind the scenes? Not important enough to be worth the effort?

like image 676
Ðаn Avatar asked Mar 17 '09 00:03

Ðаn


2 Answers

The official site for feature requests is http://connect.microsoft.com/VisualStudio.

There has already been a request for extension properties here.

Microsoft's answer on 7/29/2008 included the following:

Extension properties are a common request, and we actually got quite far with a design for the next version of the language, but ultimately had to scrap it because of various difficulties. It is still on our radar.

like image 88
Jason Kresowaty Avatar answered Nov 13 '22 14:11

Jason Kresowaty


Generally I think this would encourage poor practice.

Properties are supposed to represent some kind of state about the object in question, whereas methods should represent units of work. But many developers tend to put computationally intensive or relatively long-running code in the getters and setters where they would be much more appropriate as methods.

Extending an object is not the same as deriving from it. If you need to add properties, from a philosophical perspective you're talking about needing to add stateful information to the object. That should be done by deriving from the class.

like image 32
Rex M Avatar answered Nov 13 '22 14:11

Rex M