我有一个动态数组字符串的对象,我已经实现如下:
public class MyThing {
public int NumberOfThings { get; set; }
public string _BaseName { get; set; }
public string[] DynamicStringArray {
get {
List<string> dsa = new List<string>();
for (int i = 1; i <= this.NumberOfThings; i++) {
dsa.Add(string.Format(this._BaseName, i));
}
return dsa.ToArray();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在早些时候变得更冷,并实现了一些在LINQ中自动处理格式化数组列表的东西,但我设法失败了.
作为我尝试的事情的一个例子:
int i = 1;
// create a list with a capacity of NumberOfThings
return new List<string>(this.NumberOfThings)
// create each of the things in the array dynamically
.Select(x => string.Format(this._BaseName, i++))
.ToArray();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它确实不是非常重要,而且性能方面可能实际上更糟糕,但我想知道是否有一种很酷的方法来构建或发布LINQ扩展中的数组.
Bac*_*cks 11
Will Range有帮助吗?
return Enumerable
.Range(1, this.NumberOfThings)
.Select(x => string.Format(this._BaseName, x))
.ToArray();
Run Code Online (Sandbox Code Playgroud)