开发者

Can I use collection initializers with a NameValueCollection?

Is there a way to initialize a NVC using C# collection initializer syntax:

NameValueCollection nvc = new N开发者_运维百科ameValueCollection() { ("a", "1"), ("b", "2") };

Thanks


Yes; just uses braces instead of parentheses.

var nvc = new NameValueCollection { {"a", "1"}, {"b", "2"} };

You can call Add methods with arbitrary sets of parameters using the syntax.


You can use collection initializers with everything that has Add method. Yeah, duck typing. If Add has more then 1 param put tuples in curly bracets:

NameValueCollection nvc = new NameValueCollection() { { "a", "1" }, { "b", "2" } };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜