0
0
C Sharp (C#)programming~15 mins

Method declaration and calling in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Method declaration and calling
📖 Scenario: You are creating a simple program to greet users by their name. This program will use a method to print a greeting message.
🎯 Goal: Build a C# program that declares a method called GreetUser which takes a string parameter name and prints a greeting message. Then call this method with a specific name.
📋 What You'll Learn
Declare a method named GreetUser that takes one string parameter called name.
Inside the method, print the message Hello, {name}! using the parameter.
Call the GreetUser method from the Main method with the argument "Alice".
💡 Why This Matters
🌍 Real World
Methods help organize code into reusable blocks, making programs easier to read and maintain.
💼 Career
Understanding method declaration and calling is essential for any software development job using C#.
Progress0 / 4 steps
1
Create the Main method and class
Create a class called Program and inside it create a Main method with the signature static void Main().
C Sharp (C#)
Need a hint?

The Main method is the starting point of a C# program.

2
Declare the GreetUser method
Inside the Program class but outside the Main method, declare a static method called GreetUser that takes a string parameter named name and returns void.
C Sharp (C#)
Need a hint?

Remember to place the method inside the class but outside the Main method.

3
Add greeting message inside GreetUser
Inside the GreetUser method, write a line to print the message Hello, {name}! using Console.WriteLine and string interpolation.
C Sharp (C#)
Need a hint?

Use $"Hello, {name}!" inside Console.WriteLine to insert the name.

4
Call the GreetUser method from Main
Inside the Main method, call the GreetUser method with the argument "Alice".
C Sharp (C#)
Need a hint?

Call the method by writing GreetUser("Alice"); inside Main.