Lesson Weekend

So far we've experimented with C# by typing basic code into the REPL. If we want to write entire programs, we'll need to save our code in files. In this lesson we'll discuss how to configure, write, and run basic C# programs.

Compiling


C# is an AOT (Ahead Of Time) compiled language, unlike JavaScript, which is a JIT (Just In Time) language. We'll be discussing this more soon.

A compiler is a program or service that converts written code to machine code. AOT compilers convert code before it needs to be run and then stores that converted code in machine-readable files.

Let's create a standard "Hello World" program to see how the compiler works.

Application Setup


Every C#/.NET 5 program includes a minimum of 2 files:

  • A project file
  • A code file

These two files work together to tell the compiler how to compile our code. For this project, we'll do the following:

  • Create a new hello_world directory.
  • Add a Hello.csproj file to the directory.
  • Add a Hello.cs file to the directory. Note that both files should be capitalized.

The project file

The project file includes basic information about our project. In .NET 5, all project files end with the .csproj extension.

Hello.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>

This is a boilerplate project file. Boilerplate code is code that follows a template and can be reused elsewhere, which means we could reuse this code for other projects as well.

  • <OutputType>Exe</OutputType> tells the compiler we're building an executable file.
  • <TargetFramework>net5.0</TargetFramework> instructs the compiler to use the .NET 5 framework to build this file.

The code file

The code file includes our actual C# code and always ends in .cs We're not limited to just one code file, but for now we'll keep it simple.

Hello.cs
using System;

class Program
{
  static void Main()
  {
    // program code goes here
  }
}

using Directives

The first line using System; is called a directive. Because C# is compiled, we need to tell it what code it needs in order to function.

Cooking is a useful analogy here. When we cook, we don't get out every ingredient in the kitchen. It's more efficient to only collect the ingredients we're going to use. Remember, C# is concerned with speed. Directives are like an ingredient list and allow us to retrieve and organize only the ingredients we need. It keeps our compiled programs smaller and faster.

static void Main()

Program code resides in the brackets under the static void Main() method within the Program class. This method is automatically run when we launch any C# program. Methods that automatically run when a program is launched are called entry points.

Don't worry about other code or keywords (like static or void) yet. We'll cover these later.

Adding Source Code

Let's add some source code to our Hello World project. We'll insert the following code into the Main() entry point:

Hello.cs
using System;

class Program
{
  static void Main()
  {
    Console.WriteLine("Hello World");
  }
}

We've added a new method which we call on the Console object: Console.WriteLine(). It is similar to JavaScript's console.log() and takes a string as an argument, printing it to the terminal.

The Console object belongs to the computer, also known as the System. This is why we need to use the System in the directive we declare on the first line of the code above. Without System, we wouldn't have access to the Console object or its methods.

Compiling and Running a Program

Now we can launch our program with the following steps:

  • Navigate to the hello_world folder in the command line.

  • Run the following command:

$ dotnet build

This instructs C# to compile our code.

An obj directory and a bin directory will appear in our project directory after we run this code. These are the resources our code needs (obj) and our compiled output code (bin). Note that the obj and bin directories should be added to our .gitignore file - we don't want to push them to Github!

  • Next, we'll run the following command in our project directory to execute this compiled code:
 $ dotnet run
  • We'll then see "Hello World" in our terminal! We just wrote our first C# program!

Terminology


  • Entry Point: Code (usually a method) that's automatically run when a program is launched. In C#, this method is called Main().

Overview & Examples


Setup

Basic boilerplate setup for every C# program:

SomeFileName.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>
SomeFileName.cs
using System;

class Program
{
  static void Main()
  {
    // program code goes here
  }
}

Running Programs

  • We run a program with the following two steps:

    • Compiling: In the project directory, run > dotnet build. FileName.cs should be the name of the file containing program code. This creates an .exe file.
    • Executing: After compiling, run the following to launch the .dll file: > dotnet run.

General Guidelines

  • Files with .cs extensions contain code written by the developer.

  • Files with .csproj extensions contain project configuration data.

Lesson 1 of 5
Last updated more than 3 months ago.