Lesson 1 of 10
Your first program
- 10 min
- 3 checks
- 3 hints
- Go to task
Goal
The Main method, the Console class and string interpolation
Explanation
All C# code lives inside a class, and the program starts running at the static void Main() method. The line using System; at the top of the file brings in the ready-made tools from System, including Console.
Console.WriteLine prints text and then moves to a new line. To put a value inside a string, write a $ before the opening quote and put the value in curly braces, like this: $"Level {level}". This is called string interpolation.
Every method states the type of value it returns before its name, and the type of each parameter before that parameter's name, so static string Greet(string name) takes a string and returns a string. C# is a compiled language: the compiler checks all of your code before it runs, and if it finds even one type error, the program doesn't run at all and the compiler tells you which line the error is on.
Your task
Complete Greet(name) so it returns Hello, Layan! when the name is Layan: the words "Hello, " followed by the name and an exclamation mark. Then call it from Main to print a greeting for Layan, and press “Run and check” to see the result.
Checks
3 checks- Greet("Sara") returns "Hello, Sara!"
- Greet works with any name
- Layan's greeting was printed