Hacker News new | past | comments | ask | show | jobs | submit login

On [1], what does the underscore mean?

public SampleModule() { Get["/"] = _ => "Hello World!"; }

[1] http://nancyfx.org/




It'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-D...


As 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"];
   
  }


Unused parameter for lambda function.


This 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 ) 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).a... ) The parameter could be given any legal name, but an underscore is a convention meaning "I am not using this param".


It's dynamic path element.

    public SampleModule()
    {
        Get["/{id:int}"] = _ =>
        {
            int id = _.id;
            return "Parameter ID is: " + id.ToString();
        };
    }




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: