我想知道使用或不使用括号编写列表声明是否有区别。
我测试了两者:
List<int> ListWithParentheses = new List<int>() { 1, 2, 3 };
List<int> ListWithoutParentheses = new List<int> { 1, 2, 3 };
我得到了相同的结果。
最佳答案
可以使用参数,如
List<int> ListWithParentheses = new List<int>(capacity: 100) { 1, 2, 3 };
然后你绝对需要()
。当你有 0 个参数时,你可以使用 ()
或通过特殊规则将它们全部省略。
关于c# - 列表声明中带/不带括号的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50151494/