Writing Alphabets (A to Z) in C# using loop

Sheonarayan
Posted by Sheonarayan under C# category on | Points: 40 | Views : 61253
Have you ever wondered how to write A to Z using C# without hard coding them?

Here is the code snippet for that.

for (char c = 'A'; c < 'Z'; c++)
{
Response.Write(c + "<br />");
}


Output
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y

If you need to to write lower case just change "A" to "a" and "Z" to "z" and it will list lower case alphabets

Comments or Responses

Posted by: Karaniscool on: 11/22/2011 Level:Starter | Status: [Member] | Points: 10

This is strange to see how incrementing var c is giving the next alphabet ? Should this be giving some other result ? How this is working ?


Posted by: Niladri.biswas on: 11/23/2011 Level:Platinum | Status: [Member] | Points: 10
Also try this

Enumerable

.Range('A', 'Z' - 'A' + 1)
.ToList()
.ForEach(i => Console.WriteLine((Char)i));


Another Way

Enumerable

.Range(0, 26)
.ToList()
.ForEach(i => Console.WriteLine(Convert.ToChar(i + 65 + 0)));


For generating Small letters write i + 65 + 32

Another way

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

.ToCharArray()
.ToList()
.ForEach(i => Console.WriteLine(i));

Login to post response