Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:49:36+00:00 2026-05-29T07:49:36+00:00

I get 48 when I input 0 to a ReadLine(). Is this a bug?

  • 0

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());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:49:37+00:00Added an answer on May 29, 2026 at 7:49 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.io.*; public class ThreadSanbox { public static void main(String[] args) { Thread t1
In a related question I asked how to get input via IUP . This
I'm trying to get console input in my Clojure program, but when it gives
What's the best way to get user input in a C program where the
In this program I'm parsing input into integers and I can't seem to be
I get this error with reCaptcha: 'Input error: response: Required field must not be
I'm attempting to write a program that waits for readline input, but only for
I've been trying to get this program to work, but when I try to
When I try to convert a string to int array, i get a Null
Take this example: customer.Salary = Convert.ToDecimal(string.Format({0}! , Console.ReadLine().ToString())); (1) Why in C# we need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.