c# - 用空字符串初始化字符串数组的最短方法是什么?

标签 c# arrays string

对我来说很意外

new string[count];

填充了 null。所以我想到了

var emptyStrings = Enumerable.Range(0, count)
    .Select(a => String.Empty)
    .ToArray();

这是非常冗长的。没有捷径吗?

最佳答案

您可以使用 Enumerable.Repeat :

 string[] strings = Enumerable.Repeat(string.Empty, count).ToArray();

(但请注意,创建一个大小正确的字符串数组并进行循环会提供更好的性能。)

关于c# - 用空字符串初始化字符串数组的最短方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268506/

相关文章:

c# - 需要协助 : write a small program that will turn on the computer and executes a specified program.

ios - NSMutableArray 中的 __NSCFString 在分配它的函数之外变为 nil

javascript - 我可以将数组传递给 fromCharCode 吗

python - 如何根据冒号或连字符拆分字符串?

javascript - 在 JavaScript 中使用正则表达式替换添加 ".js"扩展

c++ - 将变量与 C++ 中的数据类型进行比较

c# - 更改 numericUpDown 上的数字时数据绑定(bind)失败

c# - 集成 Facebook 聊天

c# - 解决 LINQ 查询方法中的方法重载

java - 检查 Integer 是否在 Array Java 中不返回预期值