How will you make it if you never even try?

April 2, 2009

Nice C# idiom for parameterless lambdas

Filed under: C# — charlieflowers @ 9:35 pm

The C# syntax for a lambda with no parameters is kind of ugly:

public static void SomeMethod(Person person)
{
   Console.WriteLine( ()=> person.FirstName );
   // The ugly syntax is: ()=> person.FirstName
}

However, there is a nice, relatively new idiom springing up that makes it a little better. The idiom is to use underscore (“_”) as the parameter. Underscore is a valid name for an identifier in C#.

The convention is that, as the idiom becomes more widespread, people reading your code will know that you would never name a meaningful parameter with underscore, so therefore they know you’re going to ignore the parameter. So in effect, they understand that the intent is a parameterless lambda.

As a result, you wind up with this:

public static void SomeMethod(Person person)
{
   Console.WriteLine( _=> person.FirstName );
}

There’s another reason this idiom is appealing: its harmony with F#. F# is a language on the rise, and we’re likely to see more and more projects which have a mix of F# and C#. In F# (and many other functional languages as well), there is a key language feature called pattern matching. At an over-simplified level, it is like a case statement. And there is syntax for a “wild card” pattern that matches everything. That syntax happens to be underscore! More specifically, when used in an F# pattern match, the underscore means, “Match whatever input I’m being compared to, and I don’t plan to use the value of that input in the expression I’m about to execute.” Which is almost exactly the same meaning we’re trying to express here.

Of course, you’ll have to decide for yourself whether you like the underscore or the original ()=> syntax better. Clearly that’s a subjective matter.

Also, beware that sometimes the two aren’t interchangeable, depending on what you’re doing with the lambda. If you use the underscore idiom, then of course you really do have a single-parameter lambda. If you’re passing the lambda to a method that is going to require a zero-parameter lambda (because it takes an expression tree from the lambda or something like that), then the underscore idiom is not going to work.

You can find more info here.

6 Comments »

  1. I just did a Google Search because the author of September 2010 MSDN Article used the _ => {Code to Run} convention without explaining it which caused me to loose focus on article content. I absolutely hate it when developers do this kind of thing that is undocumented and fail to explain it. It causes a whole lot of wasted time. Sometimes I want to level people who leave obvious ambiguities in article code. It is an article cool guy… That means people are trying to learn, not be sidetracked and confused by uncommon conventions… Sometimes I hate programmers.

    Comment by Michael — September 2, 2010 @ 3:42 am

    • I hear ya’ my friend. One of those things I guess, LOL.

      Comment by charlieflowers — September 3, 2010 @ 2:33 am

    • If it was the article on delayed start task creation, thats how I got here too! Was a good article content-wise tho …

      it doesnt help that searching for “c# underscore” brings up 1000000 posts of people arguing over private member naming conventions …

      anyway i prefer ()=> for no parameters but will start using _=> for 1 parameter where the parameter isn’t used … this is the case when using a lambda to instantiate a continuation task where you don’t need to access the antecedent task in the continuation … which was its use in the article that i first saw this in hehe so I’m glad he did it, but wish he would have noted why he did it …

      Comment by travis — September 16, 2010 @ 12:00 pm

      • Was that the one along the lines of “Some examples of how Functional Programming might help a C# developer”? I think I remember skimming that article.

        Comment by charlieflowers — September 16, 2010 @ 11:42 pm

  2. Well… Maybe not hate…

    Comment by Michael — September 2, 2010 @ 3:43 am

  3. There is some interesting discussion around this point on SO at: http://stackoverflow.com/questions/424775/is-there-a-better-way-to-express-a-parameterless-lambda-than/424792?noredirect=1#comment24160197_680681
    If you do read this article please do read the accompanying discussion to get a fuller sense of this “underscore” approach

    Comment by Asad Ateeque (@aateeque) — May 29, 2013 @ 4:31 pm


RSS feed for comments on this post. TrackBack URI

Leave a reply to Michael Cancel reply

Blog at WordPress.com.