Monday, April 25, 2011

C#: Extension properties

I am pretty sure it doesn't, but... Do extention properties exist? Will they exist? Anyone heard anything? I would love it if they did... I mean, are they not just technically a get and a set method?

Now for example I would love it if I could add an extention property to DateTimeFormatInfo called ShortDateLongTimeFormat which returned ShortDatePattern + " " + LongTimePattern...

From stackoverflow
  • No they do not exist in C# 3.0 and will not be added in 4.0. It's on the list of feature wants for C# so it may be added at a future date.

    At this point the best you can do is GetXXX style extension methods.

    Jay Bazuzi : Similarly with generic properties: you have to use 'GetXXX<>' syntax.
    Svish : ok, that's what I thought. @Jay, yeah, I hate that too, hehe. Especially the inability to have a generic indexer... *sigh*
    Dan Esparza : Link to list of feature wants?
  • No, they don't exist.

    I know that the C# team was considering them at one point (or at least Eric Lippert was) - along with extension constructors and operators (those may take a while to get your head round, but are cool...) However, I haven't seen any evidence that they'll be part of C# 4. As for C# 5 and onwards - I think it's too early to speculate.

    leppie : IMO they wont add anything of value. How would you handle the setter? The purpose of properties is to 'hide the field'.
    Jon Skeet : Yes, and they could still hide the field - setting a single property might set two properties underneath, or vice versa. (Imagine something with a normal Size property, and Width/Height extension properties, or vice versa.) They'd be more useful as read-only ones though, I suspect.
    Nick : You can't bind to extension methods... being able to add your own properties for databinding could be helpful in many situations.
    leppie : Nick, that's where you use TypeCpnverter and PropertyDescriptors :)
    leppie : @Jon: Hooray for double side-effects! :) I'd rather not.
    Jon Skeet : @leppie: I agree that most of the time that's not what you want. I wouldn't be surprised to find some useful situations though. And as I say, most of the time I'd want them as read-only properties.
  • Is () too much extra to type?

    Jay Bazuzi : This question is about extension properties, but you want to challenge the merit of properties in general? Pointless.
    Svish : yes they are. cause the () to me signify a method, which is an action, a verb.
    chiccodoro : -1: If you don't have an answer, don't answer. If you use methods, you can't only add (). You have to implement two methods, a getter and a setter. The implementation takes some more effort, they don't match what you normally expect in C#, you need two different names for the methods, and the name must be a verb ("Get...", "Set...").
  • No they do not exist. As for why, I don't know...

    See also this SO topic.

  • @leppie: There are situations where extension methods just won't cut it. For instance, I wish I could define an extension property for the IgnoreAttribute type that comes with the Microsoft unit testing framework, so I can include a Reason property that gets set in the attribute. Right now, I'm using a comment to include this reason, but that is significantly more difficult to pull out of the code programmatically, than just querying a Reason property on the attribute object.

    Of course, I can understand the extra difficulty of implementing this, from Microsoft's perspective... where, for instance, will the values of these extension properties be stored? :)

    chiccodoro : @Kenneth: You're reply is supposed to be a "comment" on leppie's answer, not an individual answer. Just so you know...
    Kenneth LeFebvre : Thanks.... I'm still getting familiar with Stack Overflow.
  • Not an answer to the question but might be interesting still:

    Property extensions in C# 5.0

    // A product type in this sample has a property Name of type String
    var product = new Product();
    
    // This will print "Product.Name.IsDirty: False"
    Console.WriteLine("Product.Name.IsDurty: {0}", product.Name.IsDirty);
    
    // Now let's try to update product's Name property
    product.Name = "T-Short";
    
    // And now this will print "Product.Name.IsDirty: True"
    Console.WriteLine("Product.Name.IsDurty: {0}", product.Name.IsDirty);
    
    // Then when you try to save this product in database, it will be easier
    // for your Store Repository detect which fields must be saved
    // and which can be skipped
    
    storeRepository.Add(product);
    storeRepository.SubmitChanges();
    
    Anthony Pegram : While interesting, this question deals with the property equivalent of extension methods. The keywords here may match, but they are of an entirely different meaning!
    Scott Dorman : The blog post referenced is complete supposition and represents a *possible* (although highly unlikely) implementation. Extension properties have been considered several times before and don't "make the cut" to be included in the language and probably won't for a good while since there are usually bigger features that have much larger impact.
  • This is why they don't exist.

    • Not enough time
    • Turned out to not really solve the problems they were thought to be solving

    http://blogs.msdn.com/b/ericlippert/archive/2009/10/05/why-no-extension-properties.aspx

0 comments:

Post a Comment