Ask for Name and Greet the User in C#
Table of Contents
- Ask for Name and Greet the User in C#
- Introduction
- Objectives
- Step-by-Step Instructions and Beginner-Friendly Code
- Step 1. Prepare your folder
- Step 2. Open VS Code
- Step 3. Open Terminal
- Step 4. Create a new console project
- Step 5. Go inside the project folder
- Step 6. Write the code
- Step 7. Run the program
- Expected Output
- Code Explanation
- Summary
- Quiz (Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introduction to Exercises
- Exercises
- Assessment
- Lab Exam
Introduction
In programming, interacting with users is essential. Up to this point, most of our programs have been focused on simply displaying fixed messages to the console. While this is a great start, real-world applications are more dynamic—they receive input from users, process it, and provide meaningful output.
In C#, you can capture user input using the Console.ReadLine()
method, which lets you type something in the console and store it in a variable. After receiving this input, you can respond by displaying a customized message back to the user with Console.WriteLine()
.
For example, instead of always printing “Hello, World!”, we can create a program that asks the user for their name and then greets them personally, like saying “Hello, Maria!”. This makes programs more interactive, friendly, and human-centered, which is what real applications aim to achieve.
This lesson will introduce input and output operations in C#. You will also learn about type conversion, which allows us to handle different kinds of data, such as text, numbers, and decimal values.
By mastering these basic concepts, you will gain a strong foundation for creating dynamic programs that adapt to the user’s actions.
Objectives
In this lesson, you will develop your understanding and skills in user interaction by focusing on four key outcomes. These objectives will help you understand, learn, practice, and apply programming concepts effectively.
Introduction to Objectives:
The goal of this lesson is to help you not just memorize code, but truly comprehend why and how it works. By following the steps below, you will gradually progress from simply understanding the theory to applying it in actual programs.
- Understand:
Explain the concept of input and output in C# and describe howConsole.ReadLine()
andConsole.WriteLine()
are used to create interactive programs. - Learn:
Demonstrate how to declare variables, capture user input, and display customized output using proper syntax and type conversion. - Practice:
Build a simple program that asks for the user’s name and greets them personally, then experiment with different inputs. - Apply:
Modify the program to include additional information such as age or hobby, and implement type conversion to handle numeric inputs properly.
Step-by-Step Instructions and Beginner-Friendly Code
Follow these steps to create a C# program that asks for the user’s name and greets them.
Step 1. Prepare your folder
- Go to Drive D: on your computer.
- Create a folder with this format:
2025-1_lastname_bsis_1a
Example:
2025-1_doe_bsis_1a
- Inside this folder, store all your C# projects for this course.
Step 2. Open VS Code
- Launch Visual Studio Code (VS Code).
- Go to File → Open Folder, then select your course folder.
Step 3. Open Terminal
- In VS Code, click Terminal → New Terminal.
- A terminal window will appear at the bottom.
Step 4. Create a new console project
Type the following command in the terminal:
dotnet new console -n greet_user
This will create a folder named greet_user
with:
greet_user.csproj
– Project file needed for running the program.Program.cs
– The main C# file where we will write our code.
Step 5. Go inside the project folder
Type:
cd greet_user
Step 6. Write the code
Open Program.cs and replace its contents with the code below:
using System;
namespace GreetUserApp
{
class Program
{
static void Main(string[] args)
{
// Ask the user for their name
Console.Write("Enter your name: ");
string name = Console.ReadLine();
// Display a personalized greeting
Console.WriteLine("Hello, " + name + "! Welcome to C# programming.");
}
}
}
Step 7. Run the program
In the terminal, type:
dotnet run
Expected Output
Example when user types Alex
:
Enter your name: Alex
Hello, Alex! Welcome to C# programming.
Code Explanation
using System;
– This includes the System namespace, which gives us access to essential commands likeConsole.WriteLine
andConsole.ReadLine
.namespace GreetUserApp
– Namespaces help organize code into logical groups.class Program
– A class is like a container for our program’s code.static void Main(string[] args)
– The entry point of every C# program.Console.Write("Enter your name: ");
– Displays a prompt without moving to the next line.string name = Console.ReadLine();
– Captures what the user types and stores it in the variablename
.Console.WriteLine("Hello, " + name + "! Welcome to C# programming.");
– Combines text with the user’s input to display a personalized message.
Summary
In this lesson, we learned how to make programs interactive by asking the user for their input and displaying a personalized output. We used Console.ReadLine()
to capture text input and Console.WriteLine()
to display messages. This simple process forms the foundation of real-world applications where users interact with software. By practicing this skill, you can create programs that feel more dynamic, responsive, and user-friendly.
Quiz (Bloom’s Taxonomy)
- Which command is used to capture user input in C#?
A. Console.Write()
B. Console.ReadLine()
C. Console.Input()
D. Console.Get() - What is the purpose of the
string
keyword instring name = Console.ReadLine();
?
A. It creates a number variable
B. It stores text data
C. It converts input to uppercase
D. It prints output - What will be displayed if the user types “Maria”?
A. Enter your name: Maria
Hello, User!
B. Enter your name:
Maria!
C. Enter your name: Maria
Hello, Maria! Welcome to C# programming.
D. Hello, Welcome! - Which method displays output to the screen?
A. Console.Output()
B. Console.WriteLine()
C. Console.ReadLine()
D. Console.Display() - Why do we store user input in a variable?
A. To keep data safe
B. To use the input later in the program
C. To make the program faster
D. To display numbers only
Exercises, Assessment, and Lab Exam
Introduction to Exercises
Practicing what you’ve learned is the best way to master programming. The following exercises will help you gain confidence in handling user input and displaying output. Start simple, then gradually increase the difficulty to challenge yourself.
Exercises
- Modify the program to also ask for the user’s age and display:
"Hello, Alex! You are 20 years old."
- Ask for the user’s name and favorite color, then display a message like:
"Hi, Alex! Your favorite color is blue."
- Ask the user for two numbers, then display their sum.
- Create a program that greets the user with a time-specific message, such as:
"Good morning, Alex!"
- Display a goodbye message after the greeting.
Assessment
- Written Quiz: Answer the 5 multiple-choice questions.
- Performance Task: Build a program that asks for the user’s name, age, and city, then displays all the information in a single sentence.
Lab Exam
Write a C# program that:
- Asks for the user’s full name and favorite hobby.
- Displays a message like:
"Hello, Maria! Your hobby is painting."
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.