列表声明中带/不带括号的差异

  • 本文关键字:声明 列表 c# list
  • 更新时间 :
  • 英文 :


我想知道带或不带括号的编写列表声明之间是否有区别。

我测试了两者:

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 个参数时,您可以使用()或通过特殊规则将它们一起省略。

最新更新