-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
  • 57,407
  • 13
  • 129
  • 202
hadi
  • 35
  • 6

5 Answers5

14

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

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
  • 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
  • 36,032
  • 27
  • 135
  • 183
wjmolina
  • 2,491
  • 2
  • 24
  • 34
4

Converted to a character code. Try:

a = int.Parse(Console.ReadLine());
nitzmahone
  • 13,394
  • 1
  • 32
  • 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,018
  • 1
  • 10
  • 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();
     }
}