Our First C# Program

Visual Studio

For our first .NET application, we’ll need a text editor to type in our code, a compiler to get an executable from our code, and a copy of the .NET platform to run the application.

To make the 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

It’s possible to write and run .NET apps without the use of Visual Studio. .NET CLICommand Line Interface makes it possible to create .NET programs using the command line. This command-line utility comes with the installation of the .NET SDKSoftware Development Kit.

First, create a new folder with the name of the app.

> mkdir FirstProgram
> cd FirstProgram

Then, use the dotnet new command to create a .NET project inside this directory.

> dotnet new console

The instruction above creates a console application inside the current directory. Let’s inspect the contents.

> ls

There are three items: obj folder, .csproj file, which defines the project, and the Program.cs file that contains our Main method.

Now, we’re ready to compile and run our project.

> dotnet run
Hello World!

Try these commands in the following terminal. It has .NET SDK pre-installed.

Note: Because .NET SDK installation depends on the specific operating system we use, installation details are omitted here. Consult this page for installation instructions.

Terminal 1
Terminal
Loading...

The Program.cs file is always there in the default project template. Let’s explore this file in more detail.

The Program.cs file

Press + to interact
// Import the System namespace
using System;
// Declare our own namespace
namespace FirstProgram
{
// Declare a class
class Program
{
// Declare a method
static void Main(string[] args)
{
// Print to the console
Console.WriteLine("Hello World!");
}
}
}
  • At the top, we have the using statement that imports the System namespace to our file (line 2). Namespaces are logical groupings of types, and the System namespace contains fundamental .NET classes. We’re interested in namespaces primarily because they help avoid name collisions.
  • On line 5, we declare that we’ll use a custom FirstProgram namespace to contain our code. 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 (line 8).
  • C# has a C-like syntax. Each line ends with a semicolon and each block of code is enclosed in curly braces.
  • A class may contain fields, methods, properties, and other members. In this case, the Main() method (on line 11) is created by default.

The Main() method

In .NET, the Main() method is the program’s entry point. All applications, be they simple one-line programs like the one above or complex software that predicts stock prices, must have the Main() method somewhere. In the latest versions of .NET, manually creating the Main() method is optional because it’s generated by the compiler for us.

The words static and void are discussed in detail later. For now, know that the static keyword creates a static member, while void is used to indicate that a method returns no value.

We have the method parameters in parentheses(line 11). The string[] args is an array called args that stores values ​​of the string type. We don’t need it in this case, but in a real-life application, these are the parameter that pass when the program launch from the command line.

Just like a namespace or a class, methods must have a body that’s put between the curly braces. The body of a method is the functionality it performs. In our simple example, all the Main() method does is print "Hello World!" to the console (line 14).

Hit the “Run” button to execute the code!

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 so that it prints what we want.

Replace the body of the Main() method with the following code:

Console.WriteLine("Where are you from?");
string yourPlaceOfOrigin = Console.ReadLine();
Console.WriteLine($"Hello to everyone from {yourPlaceOfOrigin}");
Press + to interact
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
// Replace with the given code
Console.WriteLine("Hello World!");
}
}
}

Enter the input below

The Console.ReadLine() function reads text from the keyboard. We can enter our text below the code editor to imitate this keyboard input.

In string yourPlaceOfOrigin = Console.ReadLine();, we’re saying that we want to create a new variable of the string type and assign the value from Console.ReadLine() to it.

To use the value of this string variable inside a string literal, we use the $ sign when starting a new string and place the variable name inside the curly braces.

Note: The Console class used throughout our example is located in the System namespace. We can use this class because we’ve imported the namespace with the using statement on the top.

We’ve now created our first .NET program! It’s the first step in our journey towards becoming a .NET specialist. Let’s dive a little deeper!