C# 3.0’s syntatic sugar

In the old days, we’d write something like this:

view.Rows.Add("Type");
view.Rows.Add("Count");
view.Rows.Add("Whatever");

With C# 3.0 we can do this:

new string[] { "Type", "Count", "Whatever" }
    .ForEach(str => view.Rows.Add(str));

That is very cool syntatic sugar. Sure, this is not new news, but when you think about C# in the context of the new features available in 3.0, it really starts to feel a lot more like coding with JavaScript (which is a good thing).

0 thoughts on “C# 3.0’s syntatic sugar

  • i’ve had many disagreements with mark twain, not the least of which is his being dead in times when his wit would be well applied. (random thought. got a quota ya know)

    anyway, yeah c# 3.0 is cool. hoping to use it some time this year.

  • I’d have to say that for me it seems a step backward.

    You save 1 line of code, and it doesn’t become clearer what the code does.
    Exactly the opossite.

    There might be other things in which it does benefit but for me this is a bad example.

  • A lot of it is style, that’s for sure. It may be less “obvious” on quick glance, but has scalability benefits, and provides much greater usefulness for larger sets of repetitive operations. Imagine the same code above with eight “Rows.Add()”… The latter (with lambda expressions and such) focuses on the functionality and interaction with data, whereas the former is simply procedural.

    The best part of it is each person gets to choose to do it however they want, and the result is the same. 🙂

Leave a Reply to Mischa Kroon Cancel Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.