Using Range with various Linq methods
suggest changeYou can use the Enumerable class alongside Linq queries to convert for loops into Linq one liners.
Select Example
Opposed to doing this:
var asciiCharacters = new List<char>();
for (var x = 0; x < 256; x++)
{
asciiCharacters.Add((char)x);
}
You can do this:
var asciiCharacters = Enumerable.Range(0, 256).Select(a => (char) a);
Where Example
In this example, 100 numbers will be generated and even ones will be extracted
var evenNumbers = Enumerable.Range(1, 100).Where(a => a % 2 == 0);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents