I have an array of strings like this:
test[1] = "AA";
test[2] = "BB";
I like to do things in good ways. Now I need to iterate through the array so it looks like this:
1. "AA"
2. "BB"
etc ..
I think I can do this with a for loop and index but I am wondering if I can also do it with LINQ.
Prior to C# 6.0:
var result = test.Select((s, i) => string.Format("{0}. {1}", i + 1, s));
Starting from C# 6.0 you can use interpolated strings:
var result = test.Select((s, i) => $"{i + 1}. {s}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With