-9

this is my program that i wrote in C# in visual studio 2010 Ultimate and 2008 Team System:

class Program
{
    static void Main(string[] args)
    {
        int a=0;
        Console.WriteLine("Enter a number: ");
        a = Console.Read();
        Console.WriteLine("you Entered : {0}",a);
        Console.ReadKey();
     }
}

And this is the result:

Enter a number: 5 you Entered : 53

How this possible?

user2864740
  • 60,010
  • 15
  • 145
  • 220
hadi
  • 35
  • 7

5 Answers5

14

As the documentation clearly states, Read() returns the index of the Unicode codepoint that you typed.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The documentation also gives an example of running the return value through `Convert.ToChar`. – jltrem Oct 16 '13 at 20:25
  • I don't see "Unicode" anywhere in that page. So the Unicode part must come from the fact that c# does everything in Unicode. – gunr2171 Oct 16 '13 at 20:45
6

The behavior you observed is described in the documentation.

enter image description here

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
wjmolina
  • 2,625
  • 2
  • 26
  • 35
4

Converted to a character code. Try:

a = int.Parse(Console.ReadLine());
nitzmahone
  • 13,720
  • 2
  • 36
  • 39
0

Try this to reach your goal:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a number: ");
        ConsoleKeyInfo a = Console.ReadKey();
        Console.WriteLine("you Entered : {0}",a.KeyChar);
        Console.ReadKey();
     }
}
Bit
  • 1,068
  • 1
  • 11
  • 20
0

I'm new to C#, but as far as I know, it's unnecessary to initialize your variable a when you create it. Another way to write your code could be:

class Program
{
    static void Main(string[] args)
    {
        int a;
        Console.WriteLine("Enter a number: ");
        a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("you Entered : {0}", a);
        Console.ReadKey();
     }
}