Search code examples
c#out

Console.ReadLine add 48 to int


I get 48 when I input 0 to a ReadLine().

Is this a bug?

class Program
{
    static void Main(string[] args)
    {
        string name;
        int age;

        readPerson(out name, out age);
    }
    static void readPerson(out string name, out int age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
        Console.Write("Enter age: ");
        age = Console.Read();
        Console.WriteLine("Name: {0}; Age: {1}", name, age.ToString());
    }
}

Solution

  • According to the MSDN documentation, the Console.Read method returns:

    The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

    So, really what you're seeing is only the first character currently on the stream (i.e., characters received between the last two Enter pushes).

    During your unit testing, it appeared as if the values were shifted by 48, because it so happens that the ASCII values for the characters from '0' to '9' are, you guessed it, 48 for '0', 49 for '1', etc:

    ASCII Table

    Since you didn't specify a conversion, stream contents were "automagically" read as char values, and your call to Read() displayed their ASCII decimal equivalents.

    You can verify this using this simple test:

    static void TestRead()
    {
        int current = 0;
    
        Console.Write("Enter 1: ");
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        Console.Write("Enter 22: ");
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    }
    

    Which will result in:

    Output

    You will notice that back-to-back calls to Read() grab a single character from the stream, and give you its ASCII decimal equivalent. Also, note how Windows appends a carriage return (ASCII 13) and linefeed (ASCII 10) sequence for every stroke of the Enter key, which your program faithfully echoes back to you.

    A slight modification of this test method would help drive the point that lacking specific directions, the runtime will interpret the contents of your input stream as characters:

    static void TestReadModified()
    {
        int current = 0;
    
        Console.Write("Enter a: ");
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    
        current = Console.Read();
        Console.WriteLine("Next char: {0}", current);
    }
    

    As expected, the method above will return the ASCII value for character 'a':

    Output for a letter

    As others have already mentioned, this is easy to fix. Just inform the runtime that you want the value to be interpreted as an int. It's probably also a good idea to, at least, check that the input received is a number:

    static void readPerson(out string name, out int age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
    
        Console.Write("Enter age: ");
    
        // in this case, we could simply use tempAge (defaults to 0)
        // but it's just practice to check TryParse's success flag
        int tempAge;
        var success = Int32.TryParse(Console.ReadLine(), out tempAge);
    
        age = success ? tempAge : 0;
    
        Console.WriteLine("Name: {0}; Age: {1}", name, age);
        Console.ReadLine();
    }