Our First C# Program
Explore how to create your first C# program using the .NET platform and Visual Studio or the .NET CLI. Understand the project files, the Main method as the application entry point, namespaces, classes, and how to print output to the console. This lesson sets the foundation for writing and running simple C# applications.
We'll cover the following...
Visual Studio
For our first .NET application, we will need a text editor to write our code, a compiler to convert that code into an executable, and the .NET platform to run the application.
To make this process easier, developers often use Integrated Development Environments (IDEs) that can edit, compile, and run applications. For a .NET developer, Visual Studio is often the best choice.
Use the command line
We can write and run .NET apps without Visual Studio. The .NET CLI (command line interface) makes it possible to create .NET programs using the command line. This utility comes with the installation of the .NET SDK (software development kit).
First, we create a new folder with the name of the app.
mkdir FirstProgramcd FirstProgram
Then, we use the dotnet new command to create a .NET project inside this directory.
dotnet new console
This command creates a console application inside the current directory. Let's inspect the contents.
ls
There are three items: an obj folder, a .csproj file (which defines the project settings), and the Program.cs file (which contains our code).
Before we look at the code, let’s briefly inspect the .csproj file. This file tells the compiler how to build our application.
Line 5:
<TargetFramework>specifies that we are building this app for .NET 10.Line 6:
<ImplicitUsings>automatically imports common namespaces (likeSystem) across our entire project. This means we do not have to manually writeusing System;in every file.Line 7:
<Nullable>enables modern safety checks to help us prevent missing-data errors, which we cover later in the course.
Next, we check the contents of the Program.cs file using the cat command.
cat Program.cs
The cat command displays the text inside the file. Because we are using a modern .NET 10 template, the generated code is incredibly simple.
// See https://aka.ms/new-console-template for more informationConsole.WriteLine("Hello, World!");
Notice there are no classes, namespaces, or using statements. This is called a top-level statement (we’ll discuss this later). The compiler automatically generates the surrounding boilerplate code for us behind the scenes, and our <ImplicitUsings> setting handles the namespaces.
Click on the terminal below and try to execute the commands to create your first project.
Now, we are ready to compile and run our project.
dotnet run
The dotnet run command compiles and executes the application in the current directory. The following output appears on the screen:
Hello World!
Note: Because .NET SDK installation depends on the specific operating system we use, installation details are omitted here. Consult the official Microsoft documentation for installation instructions.
The Program.cs file
The Program.cs file is generated automatically by the default project template. Let’s explore this file in more detail.
Note: As you saw with the cat command, modern .NET versions generate a simplified file with only one line of code. To understand the fundamental building blocks of C#, we will use the standard, explicit structure for this lesson.
Line 2: We imports the
Systemnamespace, allowing us to use fundamental classes likeConsole.Line 5: We declares the
FirstProgramnamespace to organize our code.Line 8: We defines the
Programclass. Code within this class is enclosed in curly braces.Line 11: We defines the
Mainmethod, which serves as the entry point for the application.Line 14: Uses
Console.WriteLineto print “Hello World!” to the console.
Namespaces primarily help avoid name collisions. If we have another file with logically related functionality, we can use the same namespace so that interrelated classes are grouped.
Classes are declared in a similar fashion. We use the class keyword along with the name, and the contents are written between the two curly braces.
C# has a C-like syntax. Each line ends with a semicolon, and each block of code is enclosed in curly braces.
The Main() method
In .NET, the Main() method is the program's entry point. All applications, whether they are simple one-line programs or complex software, must have a Main() method defined somewhere. In the latest versions of .NET, manually writing the Main() method is optional because the compiler can generate it for us.
The words static and void will be discussed in detail later. For now, know that the static keyword creates a shared member, while void indicates that the method returns no value.
We see the method parameters in parentheses on line 11. The string[] args is an array that stores values of the string type. We do not need it in this specific example, but in a real-life application, these are the arguments passed when the program launches from the command line.
Just like a namespace or a class, methods must have a body placed between curly braces. The body of a method defines the functionality it performs. In our simple example, all the Main() method does is print "Hello World!" to the console.
Modify our code
Let’s experiment a bit. Notice that Console.WriteLine() can be used to print text to the console. Let’s make our program more dynamic by printing multiple messages.
Replace the body of the Main() method with the following code:
Console.WriteLine("Hello, World!");Console.WriteLine("I am writing my first C# program!");
Line 1: Prints the standard greeting.
Line 2: Prints a custom message. The program executes these lines in order, from top to bottom.
Now, update the Program.cs file with this new logic.
Note: The Console class used throughout our example is located in the System namespace. We can use this class because we imported the namespace with the using statement at the top of the file.
We have now created our first .NET program! This is the first step in our journey toward becoming a .NET specialist.