Basic Input and Output
Vaibhav • September 11, 2025
So far, we’ve written programs that display messages using Console.WriteLine
. That’s output. But
what if we want to make our programs interactive-so they can respond to the user? In this article, we’ll explore
how to take input from the user using Console.ReadLine
, how it works behind the scenes, and how
input and output form the foundation of all interactive programs.
Displaying Output with Console.WriteLine
You’ve already seen Console.WriteLine
in action. It’s the simplest way to display information to
the user. Let’s revisit it briefly:
Console.WriteLine("Welcome to the program!");
This line prints the message to the console and moves the cursor to the next line. If you want to print without
moving to a new line, use Console.Write
instead:
Console.Write("Enter your name: ");
Console.Write("Still waiting...");
Output:
Enter your name: Still waiting...
This is useful when you want the user to type input on the same line as the prompt.
Console.WriteLine
adds a newline after the message.
Console.Write
does not.
Reading Input with Console.ReadLine
To make your program interactive, you need to read input from the user. That’s where
Console.ReadLine
comes in. It pauses the program and waits for the user to type something and press
Enter.
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine("Hello, " + name + "!");
Here’s what happens:
Console.Write
displays the prompt without a newline.Console.ReadLine()
waits for the user to type something and press Enter.- The input is stored in a variable called
name
. Console.WriteLine
then uses that input to greet the user.
If the user types Vaibhav
, the output will be:
Hello, Vaibhav!
Console.ReadLine
always returns a string-even if the user types a
number. We’ll learn how to convert strings to other types in a later article.
Input is Always a String
It’s important to understand that Console.ReadLine
always returns a string. Even if the user types
42
, it’s stored as the text "42"
, not the number 42
.
For example:
Console.Write("Enter your age: ");
string ageInput = Console.ReadLine();
Console.WriteLine("You entered: " + ageInput);
If the user types 25
, the output will be:
You entered: 25
But ageInput
is still a string. You can’t do math with it yet. If you try to add 1 to it, you’ll
get an error. We’ll learn how to convert strings to numbers in a future article.
For now, treat all input as text. Don’t try to perform calculations with it until we cover type conversion.
Putting It All Together
Let’s write a simple program that asks for the user’s name and favorite color, then responds with a personalized message.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.Write("What is your favorite color? ");
string color = Console.ReadLine();
Console.WriteLine("Nice to meet you, " + name + "!");
Console.WriteLine("I like " + color + " too.");
}
}
Sample interaction:
What is your name? Vaibhav
What is your favorite color? Blue
Nice to meet you, Vaibhav!
I like Blue too.
This is your first interactive program. It uses both output (Console.WriteLine
) and input
(Console.ReadLine
) to create a conversation with the user.
Common Mistakes to Avoid
Beginners often run into a few common issues when working with input and output. Let’s look at them.
Forgetting to Store the Input
If you call Console.ReadLine()
but don’t store the result, you lose the input.
// ❌ This reads input but doesn't store it
Console.ReadLine();
Console.WriteLine("You said: " + ???);
Always store the result in a variable:
string input = Console.ReadLine();
Console.WriteLine("You said: " + input);
Trying to Do Math with Strings
Remember, input is always a string. If you try to do math with it, you’ll get an error.
// ❌ This won't work
string age = Console.ReadLine();
int nextYear = age + 1; // Error!
We’ll learn how to convert strings to numbers in the next chapter.
Using WriteLine Instead of Write
If you use Console.WriteLine
for prompts, the cursor moves to the next line. That’s fine, but
sometimes you want the user to type on the same line.
// This puts the input on a new line
Console.WriteLine("Enter your name:");
string name = Console.ReadLine();
Better:
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Use Console.Write
for prompts so the user can type on the same
line. It makes the interaction feel smoother.
Summary
In this article, you learned how to make your C# programs interactive using basic input and output. You now know:
- How to use
Console.WriteLine
andConsole.Write
to display messages - How to use
Console.ReadLine
to read input from the user - That all input is returned as a string-even numbers
- How to combine input and output to create a simple conversation
- Common mistakes to avoid when working with input and output
Input and output are the foundation of all interactive programs. In the next article, we’ll introduce the concept of algorithms-step-by-step instructions that solve problems-and how they relate to the programs you’re starting to build.