Coffeehouse Post

Single Post Permalink

View Thread: C# Extension Properties?
  • User profile image

    exoteric said:
    frindly1000 said:

    Still, using a pair of empty ellipses, you can still achieve the same effect, i.e. (for gets, not for sets)




    I believe in Scala you can use any method using infix notation so you might be able to express something like


    20 minutes ago


    I'm experimenting with using extension methods to achieve null-safe "object-oriented" expressions


    int? v = element.Value().Eval<int>();


    Value(), as an extension method over XElement, and Eval as an extension method over object, are both null-safe. If element is null, then the whole expression evaluates to null; if element.Value() is null, then the whole expression evaluates to null; if element.Value().Eval<int>() is not possible (format error or unsupported conversion), then the whole expression evaluates to null. 


    This property, that one can model null-safety with extension methods is absolutely adorable.


    I think the C# field/method/property system is a bit non-uniform now, but it's still very useable and powerful.

    True, but you can't use that in something like databinding (in WPF or Silverlight at least). Sometimes you just need a property.

    Having extension properties would be extremely powerful in a scenario where you use something like LinqToSql in a service, use partial classes there to make properties available in the DataContract and then use some extension properties to format some properties for databinding.

    I know I could do the latter with a valueconverter but I find that more troublesome then it needs to be.