I have an object with a dynamic array of strings, which I implemented as follows:
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();
}
}
}
I tried to be a little colder before and implement something that automatically created a formatted list of arrays in LINQ, but I managed to fail.
As an example of what I tried:
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();
In this case, it really is not that important, and it might be worse in performance, but I was wondering if there is a cool way to create or emit an array in LINQ extensions.
source
share