digitalmars.D - Properties in C# and prop_Foo
- Bill Baxter <wbaxter gmail.com> Aug 08 2009
- Michel Fortin <michel.fortin michelf.com> Aug 09 2009
- grauzone <none example.net> Aug 09 2009
- Bill Baxter <wbaxter gmail.com> Aug 09 2009
- "Steven Schveighoffer" <schveiguy yahoo.com> Aug 10 2009
Interesting thing I found out about C# properties.
The syntax
int Thing {
get { return _thing; }
set { _thing = value; }
}
is rewritten by the C# compiler into
int prop_Thing() { return _thing; }
void prop_Thing(int value) { _thing = value; }
Just thought it was interesting given all our discussions,
particularly that the syntax C# translates into is exactly what Andrei
was proposing we use for D's syntax. And I think I even said that was
fine, but let's have a different syntax for declaring that the D
compiler translates into those prop_Thing methods. Well, it turns out
that's exactly what C# does.
--bb
Aug 08 2009
On 2009-08-09 00:23:06 -0400, Bill Baxter <wbaxter gmail.com> said:Dang I remembered wrong from what I read yesterday. C# turns it into int get_Thing() { return _thing; } void set_Thing(int value) { _thing = value; } I don't recall anyone proposing exactly that for D. There was prop_Thing, and there was getThing. So what C# uses is a mix of those.
This has been proposed by Andrei at some point. What's different in C# from the get/set_thing proposal (well, not 100% sure but almost) is that the compiler doesn't infer the property from the presence of a getter or setter with the right name, you must explcitly tell it there's a property for a given name. I'd be equivalent to: int get_Thing(); void set_Thing(int); property Thing (getter: get_Thing; setter: set_Thing); where "property thing" reserves the symbol "thing" for a property with the given getter/setter. (Turns out that this example is pretty much how properties are defined in Objective-C.) -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Aug 09 2009
Bill Baxter wrote:Interesting thing I found out about C# properties. The syntax int Thing { get { return _thing; } set { _thing = value; } } is rewritten by the C# compiler into int prop_Thing() { return _thing; } void prop_Thing(int value) { _thing = value; } Just thought it was interesting given all our discussions, particularly that the syntax C# translates into is exactly what Andrei was proposing we use for D's syntax. And I think I even said that was fine, but let's have a different syntax for declaring that the D compiler translates into those prop_Thing methods. Well, it turns out that's exactly what C# does.
C# doesn't allow you to directly call the accessors (which are named set_Thing and get_Thing, btw.). It also creates and associates metadata with the property (so that you know that "Thing" is supposed to be a property with these setters and getters). In C# (as in the language) properties still work as cast into concrete; it's just that on the lowest level, it is mapped to normal methods + extra metadata. That's probably not quite what was proposed.--bb
Aug 09 2009
On Sun, Aug 9, 2009 at 11:25 AM, grauzone<none example.net> wrote:Bill Baxter wrote:Interesting thing I found out about C# properties. The syntax int Thing { =A0 get { return _thing; } =A0 set { _thing =3D value; } } is rewritten by the C# compiler into int prop_Thing() { return _thing; } void prop_Thing(int value) { _thing =3D value; } Just thought it was interesting given all our discussions, particularly that the syntax C# translates into is exactly what Andrei was proposing we use for D's syntax. =A0And I think I even said that was fine, but let's have a different syntax for declaring that the D compiler translates into those prop_Thing methods. =A0Well, it turns out that's exactly what C# does.
C# doesn't allow you to directly call the accessors (which are named set_Thing and get_Thing, btw.). It also creates and associates metadata w=
the property (so that you know that "Thing" is supposed to be a property with these setters and getters). In C# (as in the language) properties still work as cast into concrete; i=
just that on the lowest level, it is mapped to normal methods + extra metadata.
I see. I thought you could call get_Thing, set_Thing directly. --bb
Aug 09 2009
On Sun, 09 Aug 2009 15:53:23 -0400, Bill Baxter <wbaxter gmail.com> wrote:On Sun, Aug 9, 2009 at 11:25 AM, grauzone<none example.net> wrote:Bill Baxter wrote:Interesting thing I found out about C# properties. The syntax int Thing { get { return _thing; } set { _thing = value; } } is rewritten by the C# compiler into int prop_Thing() { return _thing; } void prop_Thing(int value) { _thing = value; } Just thought it was interesting given all our discussions, particularly that the syntax C# translates into is exactly what Andrei was proposing we use for D's syntax. And I think I even said that was fine, but let's have a different syntax for declaring that the D compiler translates into those prop_Thing methods. Well, it turns out that's exactly what C# does.
C# doesn't allow you to directly call the accessors (which are named set_Thing and get_Thing, btw.). It also creates and associates metadata with the property (so that you know that "Thing" is supposed to be a property with these setters and getters). In C# (as in the language) properties still work as cast into concrete; it's just that on the lowest level, it is mapped to normal methods + extra metadata.
I see. I thought you could call get_Thing, set_Thing directly.
It used to be in C++.NET (at least the .NET 1.0 version) you HAD to call/define properties that way ;) I think they've since added syntax to C++.Net to have more friendly property access. I'm not sure if you can still call the get_/set_ versions, but knowing Microsoft's propensity for backwards compatibility, you probably can... I too thought you could call the get_Thing and set_Thing directly from C#, though I've never found a need to. One possible reason is to pass it as a delegate. -Steve
Aug 10 2009









Michel Fortin <michel.fortin michelf.com> 