locked
Confusing construction of RouteValeDictionary RRS feed

  • Question

  • User1922541779 posted
    public static void RegisterRoutes(RouteCollection routes)
    {
        Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
        reportRoute.Defaults = new RouteValueDictionary { { "locale", "en-US" }, { "year", DateTime.Now.Year.ToString() } };
        reportRoute.Constraints = new RouteValueDictionary { { "locale", "[a-z]{2}-[a-z]{2}" }, { "year", @"\d{4}" } };
        reportRoute.DataTokens = new RouteValueDictionary { { "format", "short" } };
        routes.Add(reportRoute);
    }

    I cannot understand how  this works. is it using arrays of string?

    Friday, October 11, 2019 10:15 PM

Answers

  • User-474980206 posted

    a dictionary is a key / value pair. C# has added initialization sugar to allow dictionaries to be initialed without the

       new objectType { prop1 = value1, prop2 = value2}

    because the type and prop names can be inferred its just,

      {value1, value2}

    you could already initial collections with

        var col = new MyCollection {item1, item2}

    and the () is optional. 

       

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 12, 2019 2:15 AM