23
C# For Beginners - Lesson 1: Hello, World!
At the end of this lesson, you should be able to answer the following:
- What is a statement?
- How do I display a message on the screen using C#?
Let's make our program print a message. We're going to print out "Hello, World!" to the console.
A console (also known as terminal) is a text-only display or environment that enables us to interact with the computer.
Did you know?
The "Hello, World!" program is the traditional first program for programming languages. It is often the first program written by people who are learning to code.
To make our C# program print a message, we'll use a command called Console.WriteLine()
.
The message you want to display goes inside the round brackets ()
. We also need to put the message inside double quotes, like this:
"Hello, World!"
In the code box on your notebook, type the following:
Console.WriteLine("Hello, World!");
This line of instruction is called a statement. C# programs are made up of statements.
Statements end with a semicolon (;
). Think of a statement like a sentence - the semicolon is equivalent to the full stop or period.
If there were no full stops, the reader (in our case, the C# compiler) will have trouble understanding us, since it doesn't know where our sentences begin and end.
Next, run the program by clicking on the Execute button on the code box.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--bPxgIboj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imlefcbfurepubz9mnoq.png)
The output of the program will be shown at the bottom of the code box.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--LrAR5mr1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9g68ozph836z2vtsyqy.png)
Congratulations! You have written and run your first C# program.
Let's display another message. Write I'm learning C#!
to the console.
After the first Console.WriteLine()
, press Enter to go to a new line and type the next statement.
Console.WriteLine("I'm learning C#!");
The full program should look like this:
Console.WriteLine("Hello, World!");
Console.WriteLine("I'm learning C#!");
Run the program again by clicking the Execute button.
Your output should look like this:
![](https://res.cloudinary.com/practicaldev/image/fetch/s--YMLMi0pm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzstwm40q9gw1o2etsx8.png)
As you may have noticed, statements run in sequence. If you placed I'm learning C#!
before the Hello World!
statement, the output would be different.
Tip
You may have notice a box popping up while typing. This feature is part of IntelliSense, a helpful tool for writing code. It provides hints, suggestions, and documentation for your code.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--KetEyJan--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9m0xd0lb61d9gy3vbnpn.png)
Question
What happens when you remove the semicolon from the end of the statements? Will the program run?
Console.WriteLine("Hello World")
Console.WriteLine("I'm learning C#!")
Question
Can you spot the error in this program? (If you're stuck, copy and paste this to the code box. The squiggly red lines will show you where the error is!)
Console.WriteLine(Hello World!);
Challenges
- Introduce yourself to the computer! Display the message
Hi! My name is <NAME>
. Replace<NAME>
with your name, of course.- Print out your favourite haiku (Japanese poem) on the console. If you don't have one, use one of the examples here. Each line of the haiku should be printed on its own line.
23