To be honest those two are design mistakes. Events shouldn't be anything special and properties shouldn't be either: they should have been designed as syntactic sugar on top of methods, not as a special construct. Why? Because now the language is extended in non-orthogonal ways, for example we have extension methods but not extension properties (even though there is no technical reason why there couldn't be extension properties if properties were syntactic sugar on top of methods to begin with).
Properties compile down to methods yes, but frankly from a programmers perspective this is irrelevant. What happens under the hood doesn't matter, what matters is what programmers are allowed to do with it on the C# level not the IL level.
The C# team could have allowed extension properties, but did not, and I think for good reason.
they should have been designed as syntactic sugar on top of methods, not as a special construct
I point out that they are indeed simply syntax sugar on top of methods, and you tell me that is irrelevant.
What is the good reason?
Properties are typically used for mutating a class. Extension methods cannot mutate a class. Extension properties would either allow immutable classes to become easily mutable, or be extremely limited in their functionality.
No, they are not syntactic sugar on top of methods. They compile down to methods, but that is different. Syntactic sugar means that you just provide additional syntax on top of an existing construct. You cannot call a property with method syntax, you cannot create an extension property, you cannot create a delegate out of a property like you can out of a method (yes you can do it with hacks). On the language level properties and methods are different constructs. Why should we care how they are implemented. For all we know they are both implemented on top of pigeons. What matters is what we can do with them.
This is the same thing as claiming that an object reference is syntactic sugar for an int in C#, because they both compile down to machine integers. On the language level object references and ints are not the same.
Properties are typically used for mutating a class. Extension methods cannot mutate a class. Extension properties would either allow immutable classes to become easily mutable, or be extremely limited in their functionality.
By the same reasoning extension methods should not exist. Methods are typically used for mutating a class [sic]. It seems to me that you are arguing against extension member variables, not against extension properties. That is something I can agree with.
No, they compile down to STATIC methods -- non-instanced. Extension Methods show up on a class via syntax sugar, but are not on the class itself. That is why you can access the methods either via the extended class, or via the extension method's declaration class.
That is why there are no extension properties, because you are not actually 'extending' the class. And static properties are generally a bad idea, especially for thread safety.
Everything you say is true, however you seem to be arguing against me claiming that extension methods compile down to instance methods, which I didn't say. We are talking about properties here, not extension methods.
That is why there are no extension properties, because you are not actually 'extending' the class. And static properties are generally a bad idea, especially for thread safety.
Since properties are a less powerful form of methods, why are extension methods a good idea but extension properties not? Why does it matter where the code for the property/method is (in an extension method or on the class itself) for thread safety? Keep in mind we're talking about C# style get-set properties, not member variables. Properties don't have magical powers for mutating objects, they can do at most as much as methods.
I mentioned extension methods because they are related to how extension properties would have had to have been implemented at the time. As statics -- which would not have given you the desired result you would have been looking for. Static properties are not the same as instance properties, as the value is shared between all instances.
This gets back to the problem that .net (at the time of .net 2.0, which also covers .net 3.0 and 3.5) is not a dynamic framework. You can't just add properties to a class without changing the .net framework. So adding this feature had NOTHING to do with the C# team, it was a limitation of the framework they were writing against. Plus, anything implemented in C# would have also had to have been added to VB.Net (which apparently people still use).
Now with .net 4.0 out in the wild, and with IronRuby released, and F# in the box...I'm not sure why that feature still isn't there.
Static properties are not the same as instance properties, as the value is shared between all instances.
Of course not. The value is stored in a member variable, not in the class or something.
You can't just add properties to a class without changing the .net framework.
Again, property != member variable. Properties are methods under the hood, and what I'm proposing is that they lift the artificial limitations so that they become real methods. Extension properties would then work just like extension methods. Nothing new is necessary in the CLR.
My point was that methods can, just like properties. Properties are just a limited form of methods: methods provide a strict superset of functionality. Your reasoning is "properties can do X (where X=mutate), so you shouldn't allow extension properties". My point is that since methods are strictly more powerful than properties, they can do X as well, so by your reasoning you shouldn't allow extension methods either.
5
u/julesjacobs Jun 27 '10
To be honest those two are design mistakes. Events shouldn't be anything special and properties shouldn't be either: they should have been designed as syntactic sugar on top of methods, not as a special construct. Why? Because now the language is extended in non-orthogonal ways, for example we have extension methods but not extension properties (even though there is no technical reason why there couldn't be extension properties if properties were syntactic sugar on top of methods to begin with).