1.0: Hello World
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.
Project Set up
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 SolutionCSharpPreWork
.Check Create directory and Create new Git repository.
This will create a folder named
CSharpPreWork
, a solution (.sln file) inside that folder namedCSharpPreWork
, and a project called01_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.
Description
For our first program we will be printing Hello World to the console using the method Console.WriteLine()
Sample Code
Open
Program.cs
and type this code in theMain
method:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
Press Start at top of the window to run the program.
You should see a console window pop up with the following:
Analysis
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 useCTRL F5
instead of Start to run the program, this removes the need forConsole.ReadLine()
Challenge
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:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.WriteLine("Hey there how are you doing?");
Console.WriteLine("I'm good, how are you?");
Console.WriteLine("I'm fantastic! I'm learning how to code!");
Console.WriteLine("Wow! That's great!");
Console.ReadLine();
}
}
Last updated