While learning about Console.ReadLine() in depth, I found something very peculiar.
I wrote following code in Main method in a console application in C#
string str = Console.ReadLine();
Console.WriteLine(str);
Console.WriteLine(str.Trim());
Console.ReadLine();
When I ran the code, I gave input as ctrl+A and pressed enter. I see smileys (not able to post image, as I don't have permission to do it yet).
I want to understand, how is it showing the smiley? Shouldn't it show ^A or empty/blank (empty because when I tried debugging it, it showed string value a " ".)
If you look at that Encoding used by Console then you can see that it is used IBM437.
Now go to page : http://en.wikipedia.org/wiki/Code_page_437
You will find that 1 has Smiley.
When you press ctrl + A , it will traslated into 1.
So you get that smiley.
string str = Console.ReadLine();
Console.WriteLine((int)str[0]); // Integer value of character.
// Console.OutputEncoding ( to get further detail)
Console.ReadLine();
The smiley is just how the console displays U+0001 (start of heading) - and that's the character that apparently is read as input from the console when you type Ctrl-A... a bit like the way that if you type Ctrl-G you get U+0007 (bell).
This isn't really .NET behaviour, so much as the Windows console behaviour (both input and output) - I can reproduce exactly the same behaviour in Java, for instance.
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