You’ve learned the basics of C# console output, user input, and string manipulation. In this exercise, you put it all together by writing a program that asks the user two questions and combines their answers into a single personalized 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:
// Ask for the user's name
// Ask for the user's age
// Display a personalized message that includes both pieces of information
Remember, comments are ignored by C# when the program runs. They’re just there to help you organize your code.
// Ask for the user's name comment, add code that:
Console.Write() to display the prompt What is your name? (remember the space before the closing quote, so the user’s answer doesn’t run into the prompt)Console.ReadLine() to read the user’s response into a variable named nameTip: If you get stuck, look back at how you asked for the user’s name in the Create a personalized greeting exercise.
Select the ▶️ Run button to run the code so far, and type your name when prompted.
// Ask for the user's age comment, add code that:
Console.Write() to display the prompt How old are you? Console.ReadLine() to read the user’s response into a variable named ageNote:
Console.ReadLine()always returns astring, even if the user types a number. That’s fine for this exercise — you can use theagevariable directly inside a string interpolation without converting it to a number first.
Run the program again, and answer both prompts when asked.
Beneath the // Display a personalized message that includes both pieces of information comment, add a Console.WriteLine() statement that uses string interpolation ($"...") to combine the name and age variables into a single message.
Run your program and check that the output looks similar to this, using the name and age you entered:
What is your name? Sam
How old are you? 25
Hello, Sam! You are 25 years old.
Note: If you get an error, check that you have double quotes around each piece of text, a semicolon at the end of each statement, and that
Console,Write,WriteLine, andReadLineare capitalized correctly.
Select Clear console before you continue.
Compare your program to the example below. Your code doesn’t need to match exactly — there are many ways to write a program that works correctly — but the behavior should be the same.
// Ask for the user's name
Console.Write("What is your name? ");
string name = Console.ReadLine();
// Ask for the user's age
Console.Write("How old are you? ");
string age = Console.ReadLine();
// Display a personalized message that includes both pieces of information
Console.WriteLine($"Hello, {name}! You are {age} years old.");
Using an AI assistant (like Copilot) is a great way to explore a programming language at your own pace. Try each 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 print a line of 20 dashes in a row without typing them all out? Can you show me an example?
After the AI responds: Try using the technique it suggests to print a banner line above and below your program’s final greeting.
AI Prompt:
In C#, how can I display a message that spans multiple lines using a single Console.WriteLine() statement? Can you show me examples?
After the AI responds: Try modifying your program’s final message so the name and age each appear on their own line.