In this exercise, you write a C# program that asks for the user’s name and displays a personalized greeting. You practice using the Console.WriteLine() method to display output, the Console.ReadLine() method to collect user input, and string interpolation to format a custom message.
This exercise takes approximately 15 minutes.
You’ll write and run your code using an online C# editor — no installation required.
Open a browser and navigate to the C# editor at https://microsoftlearning.github.io/c-sharp-minor.
Before writing any logic, you’ll paste a set of guiding comments into the editor. These comments act as an outline for your program — each one marks where a specific piece of code belongs.
Copy the following comments and paste them into the editor pane:
// Display a welcome message
// Ask for the user's name
// Display a personalized greeting
Remember, comments are ignored by C# when the program runs. They’re just there to help you organize your code.
The first thing your program should do is greet the user when it starts. You’ll use the Console.WriteLine() method to display a message on the screen.
In the editor pane, add the following line beneath the // Display a welcome message comment:
Console.WriteLine("Welcome to the greeting program!");
Select the ▶️ Run button to run the code.
Check the output console. You should see:
Welcome to the greeting program!
Note: If nothing appears, make sure the code is typed exactly as shown, including the quotes, parentheses, and semicolon.
Select the Clear console button to clear the output before you continue.
Now you’ll add code that pauses the program and asks for the user’s name. You’ll use Console.Write() to display a prompt without moving to a new line, and Console.ReadLine() to wait for the user to type a response.
Beneath the // Ask for the user's name comment, add the following lines:
Console.Write("What is your name? ");
string name = Console.ReadLine();
The first line displays the prompt What is your name? in the console. The second line waits for the user to type a response and stores whatever they type into a variable called name.
Run the program again by selecting ▶️ Run.
When What is your name? appears in the output console, click on the console and type your name, then press Enter.
The program finishes after you enter your name — but it doesn’t say anything yet. You’ll fix that in the next step.
Select Clear console before you continue.
Now you’ll use the name variable to build a personalized message. You’ll do this with string interpolation — a technique that lets you embed variable values directly inside a string by prefixing the string with $ and wrapping variable names in curly braces {}.
Beneath the // Display a personalized greeting comment, add the following line:
Console.WriteLine($"Hello, {name}! It's great to meet you.");
Run the program, enter your name when prompted, and press Enter.
You should see output similar to:
Welcome to the greeting program!
What is your name? Alex
Hello, Alex! It's great to meet you.
Your complete program should now look like this:
// Display a welcome message
Console.WriteLine("Welcome to the greeting program!");
// Ask for the user's name
Console.Write("What is your name? ");
string name = Console.ReadLine();
// Display a personalized greeting
Console.WriteLine($"Hello, {name}! It's great to meet you.");
Run the program one more time and confirm the output matches what you entered. For example:
Welcome to the greeting program!
What is your name? Alex
Hello, Alex! It's great to meet you.
Using an AI assistant (like Copilot) is a great way to explore a programming language at your own pace. Try the prompt below, read the response carefully, and test the code examples it gives you in the online editor.
AI Prompt:
In C#, how can I change a string input to all uppercase or all lowercase? Can you show me examples?
After the AI responds: Look closely at the code examples it provides. Try modifying your greeting program to display the user’s name in uppercase or lowercase.