1.0: Hello World
Last updated
Last updated
In this module we'll be writing our first C# program using the console window, and we'll learn the structure of a C# file.
If you haven't already, create a folder where you would like to keep your work for this class. It's best to make this in your C drive.
Open Visual Studio Community 2017.
Go to File > New Project.
Select Windows Classic Desktop on the left sidebar and choose Console App (.NET Framework).
Browse to the folder you created in Step 1.
Name the Project 01_HelloWorld
and name the Solution CSharpPreWork
.
Check Create directory and Create new Git repository.
This will create a folder named CSharpPreWork
, a solution (.sln file) inside that folder named CSharpPreWork
, and a project called 01_HelloWorld
. Note that our image doesn't have the '01' prefix that we are asking you to add.
As you add more projects to the solution, there will be more folders added.
Note: When you need to re-open this solution, or other solutions you create, you'll open Visual Studio, go to File > Open > Project/Solution and browse to the [SolutionName].sln
file.
The Program.cs
file in HelloWorld is where you will be writing your first program.
For our first program we will be printing Hello World to the console using the method Console.WriteLine()
Open Program.cs
and type this code in the Main
method:
Press Start at top of the window to run the program.
You should see a console window pop up with the following:
We have made our first C# program. Here's what we did: 1. We called a WriteLine()
method built into the .NET framework that prints the line to the console window. More on methods later. 2. We called a ReadLine()
method that is also built into the framework that reads the current program line and also pauses the program until the user presses enter. The program doesn't necessarily need the ReadLine()
method here. It's just easier for you, the developer, because it will pause the program and keep the printed line up for you to see.
Try it without the ReadLine()
method. You can also use CTRL F5
instead of Start to run the program, this removes the need for Console.ReadLine()
To get a little bit of practice running the console and writing in C#, create a conversation between two people within your console application, and underneath the "Hello World" message. It should look something like this: