15.02.13-代码小技巧

Dictionary的初始化:

Dictionary<string, int> xx = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };

List的初始化:

List<int> yy = new List<int> { 1, 2, 3, 4 };

不用一个一个的Add,可以直接用这样的格式初始化IDictionary(?)和IList(?)的对象。

下面的RouteValueDictionary也是继承IDictionary<string,object>的,也可以用上面的格式,直接初始化值:

new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" } }

添加一个Route:

routes.Add("MyRoute", new Route("{controller}/{action}/{newid}"
    , new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" } }
    , null
    , new RouteValueDictionary { { "area", "Test" } }
    , new MvcRouteHandler())
    );
原文地址:https://www.cnblogs.com/icyJ/p/4290154.html