Posted By: ktr | Sep 11th, 2007 @ 8:58 PM
page 1 of 2
Comments: 25 | Views: 12160
ktr
ktr
two sides to everything
In C# 3 you can create an extension method like this:

public static class Extensions {
    public static string ToCamelCase(this string s) {
        // perform action
    }
}

Well what about properties? They could act as an indexer would, by using brackets around parameters:

public static class Extensions {
    public static object Cell[this DataTable t, int row, int column] {
        get { return t.Rows[row][column]; }
        set { t.Rows[row][column] = value; }
    }
}
// ...
// usage
dataTable.Cell[0, 2] = "Hello World";

That example is simple, but I think it could be a powerful concept. Take, for example, Extended Properties in WPF, which can't be added to the class. Extension Properties would allow a simpler syntax. There are also areas in the .NET Framework where a Property would be better suited but a "GetXXX(type param)" method was used because Parameterized Properties don't exist.

I dunno, it could end up cluttering the language. What do you think?
Yeah, I was hoping for extension properties too:

public static TimeSpan Minutes[this int i]
{
   get { return new TimeSpan(0, i, 0); }
}

public static DateTime Ago[this TimeSpan ts]
{
   get { return DateTime.Now.Subtract(ts); }
}

So then I could do:

DateTime d = 20.Minutes.Ago;

No ugly parentheses!
mabster wrote:
Yeah, I was hoping for extension properties too:

public static TimeSpan Minutes[this int i]
{
   get { return new TimeSpan(0, i, 0); }
}

public static DateTime Ago[this TimeSpan ts]
{
   get { return DateTime.Now.Subtract(ts); }
}

So then I could do:

DateTime d = 20.Minutes.Ago;

No ugly parentheses!


Sweet. That looks Ruby'ish (not to be mistaken by Rubish). Not that I like Ruby. Smiley
mabster wrote:
Yeah, I was hoping for extension properties too:

public static TimeSpan Minutes[this int i]
{
   get { return new TimeSpan(0, i, 0); }
}

public static DateTime Ago[this TimeSpan ts]
{
   get { return DateTime.Now.Subtract(ts); }
}

So then I could do:

DateTime d = 20.Minutes.Ago;

No ugly parentheses!
yeah but its confusing and is too close to an an indexer
TommyCarlier
TommyCarlier
I want my scalps!
How is 20.Minutes.Ago more confusing than DateTime.Now.Subtract(new TimeSpan(0, 20, 0))?
TommyCarlier wrote:
How is 20.Minutes.Ago more confusing than DateTime.Now.Subtract(new TimeSpan(0, 20, 0))?


lol. Smiley thanks for that.
It's not.  However, there are side effects.  Having the ability to write DateTime.Now.Ago is definitely confusing.  The Ago property would show up on all DateTimes (provided the using statement is there).  The idea is good, though.  Having an int extension property named MinutesAgo seems better.

I too would have liked to see extension properties.  We have custom properties that we read from the DB and are generating them onto classes.  We are forced to use the ugly GetPropertyname() syntax.
littleguru
littleguru
<3 Seattle

Holy cow... I wanted to reply but this thread is over a year old... and now I did it Sad

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Actually, as he wrote it the "Ago" property would show up on TimeSpan, not on DateTime, which makes a lot more sense. So you wouldn't actually be able to do DateTime.Now.Ago.
I meant the extension property declaration will be confusing and very close in definition to a indexer
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
The way I read it, the reason extension properties didn't make it in was because they couldn't reach consensus about a good syntax for declaring them.
SlackmasterK
SlackmasterK
I write my OWN blogging engines
Hah.  Reminds me of a query I saw recently.  At my new job, we communicate alot with an ancient DB2 system; someone posted a query like:

Select * from abc.defg
where dt = ? - 180 days

I was like, what? Awesome.

Then someone showed me that TOP 10 is now FETCH FIRST 10 ROWS ONLY.  And the awe was gone.

Don't get me started.
AndresOlivares
AndresOlivares
I am Andres

I was wondering about this feature as well. Could the syntax look something like this:

public static class Extensions {
    public static object Cell{
        get(this DataTable t, int row, int column) 
            { return t.Rows[row][column]; }
        set(this DataTable t, int row, int column) 
            { t.Rows[row][column] = value; }
    }
}

// usage
dataTable.Cell[0, 2] = "Hello World";

The usage would be the same, but the compiler would just have to translate this new syntax and it differs from indexing syntax.

public static TimeSpan Minutes
{
   get(this int i) 
    { return new TimeSpan(0, i, 0); }
}

public static DateTime Ago
{
   get(this TimeSpan ts) 
    { return DateTime.Now.Subtract(ts); }
}

They still look like properties.

Andres

I'm probably in the minority here, but I think the whole extension methods concept is confusing.  If you want to add a method to a class, then subclass it.  That's why inheritance exists, and it makes a lot more sense to me when I see MyTimeSpan.Ago rather than TimeSpan.Ago, because I might go searching the MSDN for documentation on TimeSpan.Ago only to find it doesn't exist.
>  If you want to add a method to a class, then subclass it.

What if the class you want to add a method to is sealed or worse, is a struct, preventing you from doing just that? Not unlike DateTime, TimeSpan or String?
Dr Herbie
Dr Herbie
Horses for courses
Well, without the extension method language feature, you'd have to use the decorator pattern. If you only want to add a single method, that's potentially an awful lot of work, which is why I like extensions.

For me the real strength of extension methods is in adding type-inferenced extension methods to interface, so that classes inheriting that interface automatically get interface-related functionality for free (we discussed this a while ago over on this thread).


Herbie


In regards to extension methods being confusing, I agree in that extension methods are extraordinarily powerful and thus have dramatic potential for misuse.  However there are a few cases such as adding context specific methods to base objects like object or string or adding methods to a system interface like IList where they are the best way to perform the move.

 

That being said it is sad that extension properties, which seem to me to be no more or less powerful or appropriate than extension methods were not included simply because an agreement on the declaration syntax could not be reached.

You cannot subclass a sealed class or a struct to implement the decorator pattern.

EDIT: Oops, thread necromancy.  grr, frindly1000!

exoteric
exoteric
I : Next<I>

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

 

20.Minutes().Ago()

 

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.

Coding WPF classes in C# sometimes feels like coding COM classes in C++: you have to explicitly provide plumbing that obscures the high-level abstractions.  XAML is nicer, but it's both limited in capability and saddled with clunky XML syntax.  This is my biggest problem with WPF, an API that in the abstract is very nice, I just wish there were a better view into it ... like JavaFX Script.   Tongue Out

Maddus Mattus
Maddus Mattus
Do, or do not. There is no try. - Yoda

Blend doesnt suit your needs?

I haven't used it much, but it's a visual designer, right?  There's more to WPF than visual design.  I'm thinking of setting up dependency properties, etc.

page 1 of 2
Comments: 25 | Views: 12160
Microsoft Communities