Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 25, 2011 8:32 PM
I saw this code in Joe Albahari's asynchronous sample:
void WaitForTwoSecondsAsync (Action callback) // Asynchronous NON-BLOCKING method
{
new Timer ( _ => callback()).Change (2000, -1);
}
void Main()
{
"Waiting...".Dump();
WaitForTwoSecondsAsync (() => "Done".Dump());
}
There's an underscore keyword in WaitForTwoSecondsAsync method. What does that mean?
All replies (11)
Thursday, August 25, 2011 9:01 PM ✅Answered | 4 votes
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.
public static void SomeMethod(Person person)
{
Console.WriteLine( ()=> person.FirstName );
// The ugly syntax is: ()=> person.FirstName
}
//or:
public static void SomeMethod(Person person)
{
Console.WriteLine( _=> person.FirstName );
}
Mitja
Friday, August 26, 2011 1:20 AM
Mitja,
Some thing new for me, Nicely explained....
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
Friday, August 26, 2011 4:57 PM
Is there a documentation in MSDN about this?
Friday, August 26, 2011 5:11 PM
Is there a documentation in MSDN about this?
Read Mitja's post again. It tells the whole story.
That underscore is not a keyword. It's a parameter just like any other variable.
Please mark this post as answer if it solved your problem. Happy Programming!
Friday, August 26, 2011 11:24 PM
Seems like ppl cant read any longer.Mitja
Wednesday, April 4, 2012 12:14 AM
I'm familiar with the idiom, but ironically, I consider _ => expr instead of () => expr to be the uglier syntax. To me, it almost goes out of its way to represent a placeholder, as opposed to an empty parameter. Beauty is in the eye of the beholder, I guess.
Wednesday, April 4, 2012 12:18 AM | 2 votes
@Mitja, you should give attribution to this post:
http://charlieflowers.wordpress.com/2009/04/02/nice-c-idiom-for-parameterless-lambdas/
Wednesday, April 4, 2012 11:48 AM | 2 votes
I don't see a choice here. Usually, you cannot use a parameterless lambda and a parameterful lambda interchangeably.
Wednesday, April 4, 2012 2:14 PM
I agree with Louis. I can't see how you actually have a choice.
Wednesday, April 4, 2012 2:19 PM
I'm familiar with the idiom, but ironically, I consider _ => expr instead of () => expr to be the uglier syntax. To me, it almost goes out of its way to represent a placeholder, as opposed to an empty parameter. Beauty is in the eye of the beholder, I guess.
Consider a method that takes an Action or Func with one parameter that you don't use. Oftentimes you'll have event handlers that allow for arguments to be passed in but those arguments tend to be only used on rare occasions. This idiom would make it clear that the arguments are unused, but exist.
Wednesday, April 4, 2012 2:22 PM
@servy:
Completely agree, but the point is that the choice isn't between _ and (), it's between _ and some parameter name.
Someone has mistakenly thought that you can choose between _ and () syntax, and you actually can't.