【发布时间】:2019-09-26 07:46:09
【问题描述】:
以下在性能方面有区别吗?
private static readonly string[][] a = { new string[] {"a", "b", "c"}};
private static readonly string[][] a = { new string[3] {"a", "b", "c"}};
假设我可以编写 2 个选项(我事先知道数组中的值)哪个更好/更正确?
【问题讨论】:
-
不,没有。
-
它将生成完全相同的 IL 代码,如下所示:
private static readonly string[][] a = { new [] { "a", "b", "c" } }; -
我更喜欢第一个。它更不容易出错。如果数组发生变化,如果您使用第二个版本,则必须在两个地方修改代码
-
这几乎是premature optimisation 的定义。即使这可能会“更快”,是什么让您认为这很重要?
标签: c# string allocation