how to have a list of 'a' to 'z'?

Using Linq try this to have a list from "a" to "z".

var list = Enumerable.Range('a', 'z' - 'a' + 1).Select(c => (char)c).ToList();

If you want to get in upper case, is the same but using upper case.

var list = Enumerable.Range('A', 'Z' - 'A' + 1).Select(c => (char)c).ToList();

Edit:

Your question is to get the list, but to get as dictionary you can use .ToDictionary() and initialize with value 0:

var dictionary = Enumerable.Range('a', 'z' - 'a' + 1).Select(c => (char)c).ToDictionary(i => (char)i, i => 0);

The same case to upper/lower case.


Is performance a thing? You can keep it simple.
You do not need to create your own dictionary, register every possibility then start counter[i]++ for each ocurrence, seems a bit of an overkill to me.

You can group up the letters by using .GroupBy (System.Linq), then after you can check the count of each ocurrence.

Example:

var word = "test";
var groupedLetters = word.GroupBy(x => x);

foreach(var letter in groupedLetters)
{
    Console.WriteLine($"{letter.Key} - {letter.Count()}");
}

Output:

t - 2  
e - 1  
s - 1

Here's a way to do it in LINQ:

var alphabetCounter = Enumerable.Range(97, 26).ToDictionary(i => (char)i, i => 0);

This will create a dictionary with all chars with values 0.

ASCII codes of alphabet (lowercase) begins at 97, we can then take 26 numbers from there and convert them to char.

Tags:

C#