C# 简化字符串数组初始化

标签 c# arrays linq

C# 简化字符串数组初始化。有没有办法在 C# 中优化以下代码

string[] createdBy = new string[totalRowCount];
for (long i = 0; i < totalRowCount; i++)
{
    createdBy[i] = userName;
}

还有

int?[] defaultInd = new int?[totalRowCount];
        for (long i = 0; i < totalRowCount; i++)
        {
            if (i == 0)
            {
                defaultInd[i] = 1;
            }
            else
            {
                defaultInd[i] = 0;
            }
        }

最佳答案

string[] myIntArray = Enumerable.Repeat(userName, totalRowCount).ToArray();

关于C# 简化字符串数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113242/

相关文章:

c# - 编辑 msi 数据库表

c# - 在 Unity 中以圆周运动控制/移动对象

javascript - 数组迭代只执行一次appendChild

c# - 从包含逗号分隔值的字符串数组中查找项目的索引

c# - 如何转换 'MS.Internal.Data.CollectionViewGroupInternal'使用?

c# - 如何将 TextBlock 文本属性设置为字符串资源?

javascript - Angular JS - 使用相同 ng-model 基于多个对象属性的 ng-repeat 过滤对象数组

PHP数组按值删除(不是键)

c# - 提高 LINQ 枚举器内条件查询的性能

LINQ to SQL 延迟执行和实现