Thoughts on life, liberty, and information technology

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).

4 responses to “C# 3.0’s syntatic sugar”

  1. josh Avatar
    josh

    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.

    Like

  2. josh Avatar
    josh

    ..and now I see the quote at the bottom of the page is random too. thus my random comment has absolutely no context. yay.

    Like

  3. Mischa Kroon Avatar
    Mischa Kroon

    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.

    Like

  4. brian Avatar
    brian

    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. 🙂

    Like

Leave a comment