tags:

views:

3243

answers:

13

I've wanted this for fluent interfaces. See, for example this Channel9 discussion. Would probably require also adding indexed properties.

What are your thoughts? Would the advantages outweigh the "language clutter"?

+23  A: 

Since properties are just syntactic sugar for methods, I don't see why C# should have extension methods without extension properties.

Ben Hoffstein
+3  A: 

I don't know why not. Properties are just getter/setter methods with differant syntax.

Charles Graham
+1  A: 

I guess it would be great, as long as there's no or minimal performance penalty in using them.

Leon Tayson
A: 

Would definately add to the repertoire of tools at our disposal.

Now as for my opinion, yes they should, but like anything, developers need to use it properly and when needed. Like most new features, a lot of dev's use them without fully understanding them and when/how/where/how best to use them. I know I get sucked into this sometimes too.

But ya, sure, bring it on. I could see myself using it in certain scenarios

mattlant
A: 

I'm guessing indexed properties will be a mere stocking stuffer among a large bag of of significant enhancements we'll be seeing in 4.0.

J Healy
+2  A: 

I don't think extension properties would be nearly as useful. I find myself mostly using properties to encapsulate fields (classic get; set;) or to provide read only goodness (just a get on a private, readonly, contructor-set field). Since extensions can't access private members, I don't really see the point, especially for "set;". To do anything, "set;" would just have to call other methods anyway. Then you run into the issue of properties throwing exceptions.
Since extensions are limited to using public properties and methods, I find it cleaner and easier to read code that uses a utility class. When it comes down to it, we are using extension methods to make LINQ look pretty. To keep developers from doing the wrong thing, I can deal with an extra () here and there in my LINQ and stick to just extension methods.

foson
The point is to not deal with extra (), the same point as with any other syntactic sugar
alpav
A: 

No, a property is just a way to hide what has really been generated for the code. If you look at the reflected code or the IL you can determine what you're really getting and it is the following:

public string MyProperty { get; set; }

becomes

public string get_MyProperty(){ return <some generated variable>; }
public string set_MyProperty(string value) { <some generated variable> = value; }

It's just 2 methods.

Slace
You can't databind to get_MyProperty()
recursive
I don't understand this answer - are you implying you're not an advocate for regular properties either?
David Cuccia
no, i'm just pointing out that the concept of a property is an IDE concept, the IL doesn't support something that looks like a property. You can achieve the same functionality with get_MyProperty(this object o) { ... }. Sure it's not the same syntactic sugar that we're use to but it's the same overall result.
Slace
+1  A: 

Seems like it could be easily misused. As others have mentioned, C# properties are just syntactic sugar for methods. However, implementing those as properties has certain connotations: accessing won't have side effects and modifying a property should be very inexpensive. The latter point is crucial as it seems like extension properties will almost always be more expensive than conventional properties.

Rodrick Chapman
+6  A: 

It's about databinding, let's say I have an object for binding to my UI and I want to hide show it based on other properties of the object. I could add an extension property for IsVisible or Visibility and bind that property to the UI.

You can't bind extension methods, but being able to add properties for databinding to existing types you can't extend could be very useful.

Nick
Yes, I need it for databinding also!!!!
tbone
This would not work: data binding uses reflection on the bound object's type to get at the properties. An extension property would exist in an arbitrary second type which the binding framework would have no knowledge of. Extension methods (and properties, if they existed) are a compile-time feature with no runtime discoverability.
Ben M
There are alternatives to this (I know I'm late to respond, but just saw this answer), by using collections that implement some interfaces related to databinding, you can have the collection add more bindable properties. In other words, by giving the recipe for how to calculate the IsVisible property to the collection, you can bind to it in a grid or similar. Of course, if you want to bind to it from a textbox or similar, you're probably out of luck.
Lasse V. Karlsen
+4  A: 

In my book the #1 most substantial reason for extension properties is implementing fluent interface patterns around unowned code. I built a wrapper around the NHibernate session to make it more intitutive to work with so I can do logic similar to

public bool IsInTransaction
    {
        get { return _session.Is().Not.Null && _session.Is().InTransaction; }
    }

Which looks very stupid that Is must be a method as there is no way for me to insert the Is as a property unless I directly modify the source on the session object.

Chris Marisic
A: 

Developers should use properties to organize hierarchy of names and avoid name concatenation with camel casing or underscores. For example, instead of HttpUtility.UrlDecode they should extend string class to use something like "some str".decode.url Currently the only way to do it in C# is this: "some str".decode().url

alpav
+1  A: 

Would be a nice thing to have, but I see many people saying they want it for data binding which isn't possible since it's using reflection.

JHiller
A: 

Yes, please.

And also add indexed properties and STATIC extension stuff, because I SERIOUSLY want that for System.Console (this is not meant as a joke!).

Turing Complete