C# 新字符串 [2] 与新字符串 []
C# new string[2] vs new string[]
以下在性能方面有区别吗?
private static readonly string[][] a = { new string[] {"a", "b", "c"}};
private static readonly string[][] a = { new string[3] {"a", "b", "c"}};
假设我可以编写 2 个选项(我事先知道数组中的值)哪个更好/更正确?
两者都生成相同的 IL 代码,因此性能相同,而且都是正确的。
第二个选项的唯一优点是当你需要限制元素的数量时,放多了或放少了都会给你一个编译错误。
// error
private static readonly string[][] a = { new string[3] {"a", "b"}};
以下在性能方面有区别吗?
private static readonly string[][] a = { new string[] {"a", "b", "c"}};
private static readonly string[][] a = { new string[3] {"a", "b", "c"}};
假设我可以编写 2 个选项(我事先知道数组中的值)哪个更好/更正确?
两者都生成相同的 IL 代码,因此性能相同,而且都是正确的。
第二个选项的唯一优点是当你需要限制元素的数量时,放多了或放少了都会给你一个编译错误。
// error
private static readonly string[][] a = { new string[3] {"a", "b"}};