|
| 1 | + |
| 2 | +--- |
| 3 | +lab: |
| 4 | + title: 'Create a personalized greeting' |
| 5 | + description: 'Write a C# program that uses Console.WriteLine(), Console.ReadLine(), and string interpolation to display a personalized greeting message.' |
| 6 | + level: 100 |
| 7 | + duration: 15 |
| 8 | + islab: true |
| 9 | + status: 'released' |
| 10 | +--- |
| 11 | + |
| 12 | +# Create a personalized greeting |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +This exercise takes approximately **15** minutes. |
| 17 | + |
| 18 | +## Open the online C# IDE |
| 19 | + |
| 20 | +You'll write and run your code using an online C# editor — no installation required. |
| 21 | + |
| 22 | +1. Open a browser and navigate to the C# editor at [https://microsoftlearning.github.io/c-sharp-minor](https://microsoftlearning.github.io/c-sharp-minor). |
| 23 | + |
| 24 | +2. You'll see two panels: |
| 25 | + - **Code Editor pane** (top): where you write your C# code. |
| 26 | + - **Output Console** (bottom): where output is displayed and where you can type input when the program asks for it. |
| 27 | + |
| 28 | +3. The editor may contain some default code. Select all of it and delete it so you're starting with a clean, empty file. |
| 29 | + |
| 30 | +## Set up your program |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +1. Copy the following comments and paste them into the editor pane: |
| 35 | + |
| 36 | + ```csharp |
| 37 | + // Display a welcome message |
| 38 | +
|
| 39 | + // Ask for the user's name |
| 40 | +
|
| 41 | + // Display a personalized greeting |
| 42 | + ``` |
| 43 | + |
| 44 | + Remember, comments are ignored by C# when the program runs. They're just there to help you organize your code. |
| 45 | + |
| 46 | +## Display a welcome message |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +1. In the editor pane, add the following line beneath the `// Display a welcome message` comment: |
| 51 | +
|
| 52 | + ```csharp |
| 53 | + Console.WriteLine("Welcome to the greeting program!"); |
| 54 | + ``` |
| 55 | + |
| 56 | +1. Select the ▶️ **Run** button to run the code. |
| 57 | + |
| 58 | +1. Check the output console. You should see: |
| 59 | + |
| 60 | + ```output |
| 61 | + Welcome to the greeting program! |
| 62 | + ``` |
| 63 | + |
| 64 | + > **Note**: If nothing appears, make sure the code is typed exactly as shown, including the quotes, parentheses, and semicolon. |
| 65 | + |
| 66 | +1. Select the **Clear console** button to clear the output before you continue. |
| 67 | + |
| 68 | +## Ask for the user's name |
| 69 | + |
| 70 | +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. |
| 71 | + |
| 72 | +1. Beneath the `// Ask for the user's name` comment, add the following lines: |
| 73 | +
|
| 74 | + ```csharp |
| 75 | + Console.Write("What is your name? "); |
| 76 | + string name = Console.ReadLine(); |
| 77 | + ``` |
| 78 | + |
| 79 | + 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`. |
| 80 | + |
| 81 | +1. Run the program again by selecting ▶️ **Run**. |
| 82 | + |
| 83 | +1. When `What is your name? ` appears in the output console, click on the console and type your name, then press **Enter**. |
| 84 | + |
| 85 | +1. The program finishes after you enter your name — but it doesn't say anything yet. You'll fix that in the next step. |
| 86 | + |
| 87 | +1. Select **Clear console** before you continue. |
| 88 | + |
| 89 | +## Display a personalized greeting |
| 90 | + |
| 91 | +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 `{}`. |
| 92 | + |
| 93 | +1. Beneath the `// Display a personalized greeting` comment, add the following line: |
| 94 | +
|
| 95 | + ```csharp |
| 96 | + Console.WriteLine($"Hello, {name}! It's great to meet you."); |
| 97 | + ``` |
| 98 | + |
| 99 | +1. Run the program, enter your name when prompted, and press **Enter**. |
| 100 | + |
| 101 | +1. You should see output similar to: |
| 102 | + |
| 103 | + ```output |
| 104 | + Welcome to the greeting program! |
| 105 | + What is your name? Alex |
| 106 | + Hello, Alex! It's great to meet you. |
| 107 | + ``` |
| 108 | + |
| 109 | +1. Your complete program should now look like this: |
| 110 | + |
| 111 | + ```csharp |
| 112 | + // Display a welcome message |
| 113 | + Console.WriteLine("Welcome to the greeting program!"); |
| 114 | + |
| 115 | + // Ask for the user's name |
| 116 | + Console.Write("What is your name? "); |
| 117 | + string name = Console.ReadLine(); |
| 118 | + |
| 119 | + // Display a personalized greeting |
| 120 | + Console.WriteLine($"Hello, {name}! It's great to meet you."); |
| 121 | + ``` |
| 122 | + |
| 123 | +1. Run the program one more time and confirm the output matches what you entered. For example: |
| 124 | + |
| 125 | + ```output |
| 126 | + Welcome to the greeting program! |
| 127 | + What is your name? Alex |
| 128 | + Hello, Alex! It's great to meet you. |
| 129 | + ``` |
| 130 | + |
| 131 | +## Code with AI |
| 132 | + |
| 133 | +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. |
| 134 | + |
| 135 | +### Discovery: Transforming text |
| 136 | + |
| 137 | +**AI Prompt:** |
| 138 | +```In C#, how can I change a string input to all uppercase or all lowercase? Can you show me examples?``` |
| 139 | + |
| 140 | +**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. |
| 141 | + |
| 142 | +<details> |
| 143 | +<summary>Show answer</summary> |
| 144 | +You can use the following string methods to transform text: |
| 145 | + |
| 146 | +```csharp |
| 147 | +string name = Console.ReadLine(); |
| 148 | + |
| 149 | +// Converts to all uppercase |
| 150 | +string uppercaseName = name.ToUpper(); |
| 151 | + |
| 152 | +// Converts to all lowercase |
| 153 | +string lowercaseName = name.ToLower(); |
| 154 | +``` |
| 155 | +</details> |
0 commit comments