23 February 2010 9:56p Pacific

How about jQuery-style “property methods” for C#?

by Matt Sherman

Here’s an idiom that I think would be useful in C#: “property methods” in the style of jQuery.

There are many methods in jQ – html(), val(), width() – that are getters when they are called without an argument, and setters when they are. This makes for some very nice fluent code:

$(“#someElement”).width(“100px”).html(“hello there”);
window.alert(“The width of #someElement is ” + $(“#someElement”).width());

I’d love to be able to do this in C#, to bring in some of that fluency and terseness. For example, I currently have to write:

myPerson.FirstName = “Joe”;
myPerson.LastName = “Smith”;
myPerson.ZipCode = “12345”;

The code smell here is that I refer to myPerson 3 times. Don’t you just wanna say “Yeah, I get it, myPerson, sheesh.” Wouldn’t it be better to write:

myPerson.FirstName(“Joe”).LastName(“Smith”).ZipCode(“12345”);

Of course I could implement the above with overloads and private fields. But I shouldn’t need to do that much notation.

I think it can be done in C# without new keywords. Simply allow methods to have get and set bodies:

public class Person
{
public string FirstName() { get; set ; }
public string LastName () { get; set ; }
public string ZipCode () { get; set ; }
}

The compiler would interpret these much like regular properties, backed by (automagical) private fields, with the caveat that a passed value invokes set. The setter returns the parent object, giving us the fluency.

And as with properties, you could add bodies to get and set as you desire, where the value argument is implied for set.

Overall, this is the equivalent of:

public class Person
{
public string FirstName()  // getter
{
   return _FirstName;
}

public Person FirstName(string value)   // setter
{
   _FirstName = value;
   return this;               // the fluent bit
}

private string _FirstName;   // backing store

//…and repeat for each property
}

Again, I could write my classes this way. But that would be so much boilerplate, uninformative code. Please compiler, do it for me.

Comments

24 February 2010 3:06p Pacific #

Ian Patrick Hughes
Hey Matt,

I am pretty sure this exists in C# 4. Lemme see if I can find a link with a good break down.


Ian Patrick Hughes United States |

24 February 2010 3:23p Pacific #

Ian Patrick Hughes
Yeah, well, I was close. It's the C# Optional Parameters ( mtaulty.com/.../c-and-optional-parameters.aspx )

It does work with a ".property()" syntax like works based upon the properties in a given class object BUT without the pug-fugly multiple overloads. So, your person sample would go:

Person p = new Person (FirstName: "Joe", LastName: "Smith", ZipCode: "12345");

C++ already had this, but its a nice feature and you're right. It's sorely needed.

Ian Patrick Hughes United States |

25 February 2010 1:25p Pacific #

Matt Sherman
Yeah, I dig the optional parameters thing. I means writing *many* fewer overloads.

But it's not quite the same as what I suggest. I want properties to act like jQ methods. Eric Lippert kinda poo-pooh'd it though, doesn't fit the language conventions in his view.

Matt Sherman United States |

Comments are closed

Tell others

TwitterTweet this page
Digg!Digg this page
TwitterAdd to Google Reader

Experimental! Let me know how it works for you.

Shorten this page's URL

Learn more about the TinyASP URL shortener

ASP.Net jQuery Controls

Implement jQuery effects using familiar ASP.Net server controls. Learn more...

Recent posts

Avoiding “magic strings” in jQuery, C# and ASP.net MVC

Alikewise learnings #1: DIY PR

Sherman’s law of prior knowledge, or, predicting the past

The busiest people at Apple right now…

When “infographics” jump the shark

HTTPS is the least of your problems

Stacking up

Beware the truth-tellers

more...  

About us

ClipperHouse.com is brought to you by Matt Sherman and Fernando Chilvarguer, among others. Contact us here.