5 ms·
On [1], what does the underscore mean? public SampleModule() { Get["/"] = _ => "Hello World!"; } [1] http://nancyfx.org/ http://nancyfx.org/
by yawgmoth 13y ago
On [1], what does the underscore mean?
public SampleModule()
{
Get["/"] = _ => "Hello World!";
}
[1] http://nancyfx.org/ http://nancyfx.org/
- ntnj 13y agoUnused parameter for lambda function.
- SideburnsOfDoom 13y agoThis is the most accurate reply. The expression Get["/"] = _ => "Hello World!"; is actually of the form a = b; Where "a", i.e. "Get["/"]" is an array-like property (http://msdn.microsoft.com/en-us/library/2549tw02.aspx http://msdn.microsoft.com/en-us/library/2549tw02.aspx ) and the "b" value assigned to it is an anonymous function with one parameter, i.e. _ => "Hello World!" In Nancy these handlers are of the type Func<dynamic, dynamic> ( http://msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/bb549151(v=vs.110).a... ) The parameter could be given any legal name, but an underscore is a convention meaning "I am not using this param".
- damianh 13y agoIt's the 'Dynamic Dictionary' [0]. It's usually called 'parameters' but it's idiomatic in dotnet to name lambda input parameter '_' if don't use it in your expression. [0] https://github.com/NancyFx/Nancy/wiki/Taking-a-look-at-the-DynamicDictionary https://github.com/NancyFx/Nancy/wiki/Taking-a-look-at-the-D...
- romanovcode 13y agoIt's dynamic path element. public SampleModule() { Get["/{id:int}"] = _ => { int id = _.id; return "Parameter ID is: " + id.ToString(); }; }
- JohnBooty 13y agoAs others have said, it's idiomatic to name a lambda param "_" if you're not using it. To perhaps clarify a bit, here's what the Hello World example would look like if we added a second route that uses the lambda parameter, and a third route that does something else, and a fourth route that renders a view template. public SampleModule() { Get["/"] = _ => "Hello World!"; Get["/hello/{name}"] = p => ("Hello, " + p.name + "!"); Get["/something-complicated"] = _ => { // complicated stuff happens here return "OK... we're all done"; }; // A variety of template engines are supported Get["/my-resume"] = _ => View["resume.cshtml"]; }