Skip to content
Educora
Beginner12 min1 / 16

What is C#? .NET and your first program

Find out what C# and the .NET platform are and where they are used, and write your first program.

Check yourself
In this lesson you will learn
  • Explain what C# and .NET are and where they are used
  • Print text and the results of calculations with Console.WriteLine
  • Run a program with dotnet run, write comments and read compiler errors

Games made with Unity, online shop websites, desktop programs for Windows, servers running in the cloud — all of them can be written in the same language: C# (pronounced “C sharp”). In this lesson you will find out what C# is, meet the .NET platform behind it and write your first program.

C# and .NET

C# is a modern, object-oriented programming language created by Microsoft. It was designed by a team led by Anders Hejlsberg, and its first version was released in 2002. C# is statically typed: the type of every value is known in advance, so the compiler catches many mistakes before the program even runs.

Definition
.NET

Microsoft's free, open-source platform for building and running C# programs. It works on Windows, macOS and Linux and comes with a huge set of ready-made libraries.

A computer does not understand your code directly. First the C# compiler translates it into an intermediate language (IL); then, when the program starts, the .NET runtime (the CLR) turns that code into machine code for the processor. That is why the same program can run on different operating systems.

Where is C# used?

  • Web — websites, web APIs and online services with ASP.NET Core.
  • Games — scripts in the Unity game engine are written in C#, and a great many mobile and PC games are made with Unity.
  • Desktop apps — Windows applications with WPF and Windows Forms, and with .NET MAUI mobile apps as well.
  • Cloud — services running on cloud platforms such as Microsoft Azure.
  • Console programs — small text-based tools. These are exactly what we will write in this course.

Your first program

By programmers' tradition, the first program in a new language prints “Hello, world!” on the screen. In modern C#, one line is enough:

C#
Console.WriteLine("Hello, world!");
Expected output
Hello, world!
  • Console is a ready-made class for working with the console (the text window).
  • WriteLine is its method: it writes the value in the parentheses and moves to a new line. The dot connects the class and the method.
  • "Hello, world!" is text in double quotes — a string.
  • ; — the semicolon: every statement in C# ends with it.

This style is called top-level statements and has existed since C# 9 (2020). In older code and books you will see the same program in a longer form, with a Program class and a Main method where the program starts. Behind the scenes, the compiler turns the short version into exactly this kind of code.

C#
namespace HelloApp;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, world!");
    }
}
Expected output
Hello, world!
The classic form: the program starts in the Main method.

How do you run a program?

  1. 1
    Install the .NET SDK

    Download the free .NET SDK from Microsoft's official website. For writing code, Visual Studio, Visual Studio Code with the C# Dev Kit extension or JetBrains Rider all work well.

  2. 2
    Create a project

    In a terminal, type dotnet new console -o HelloApp. A Program.cs file appears in the HelloApp folder.

  3. 3
    Run it

    Go to the folder with cd HelloApp and type dotnet run. The program is compiled and Hello, World! appears on the screen.

  4. 4
    Or just one file

    Starting with .NET 10 you can even skip the project: put your code into an app.cs file and run dotnet run app.cs.

Terminal
dotnet new console -o HelloApp
cd HelloApp
dotnet run
Commands typed in the terminal

Text and calculations

Console.Write prints text without moving to a new line, while Console.WriteLine moves the cursor to the next line after printing. Strings can be joined with the + sign. The program runs its statements from top to bottom, one by one.

C#
Console.Write("Salam, ");
Console.Write("Aysel!");
Console.WriteLine();
Console.WriteLine("I live in Baku.");
Console.WriteLine("I am " + 12 + " years old.");
Expected output
Salam, Aysel!
I live in Baku.
I am 12 years old.

C# is also a good calculator. Notice that "2 + 3" in quotes is just text, while 2 + 3 without quotes is calculated. When you divide two whole numbers with /, the result is a whole number too — the fractional part is simply dropped. The % operator gives the remainder.

C#
Console.WriteLine(2 + 3);
Console.WriteLine("2 + 3");
Console.WriteLine(7 * 8);
Console.WriteLine(17 / 5);
Console.WriteLine(17 % 5);
Console.WriteLine("Total: " + (45 + 30));
Expected output
5
2 + 3
56
3
2
Total: 75

Comments and errors

The compiler ignores comments: they are for the people who read the code. // turns everything up to the end of the line into a comment, and /* ... */ can span several lines.

C#
// This program greets the learner
Console.WriteLine("Welcome to Educora!"); // greeting
/* A comment on
   several lines */
// Console.WriteLine("This line is switched off");
Console.WriteLine("C# is fun!");
Expected output
Welcome to Educora!
C# is fun!

When the compiler finds a mistake, it does not run the program and shows a message with an error code, for example CS1002. The message also gives the line and column number, so the mistake is easy to find.

CodeErrorWhy
Console.WriteLine("Hi")CS1002: ; expectedThe semicolon at the end is missing.
console.WriteLine("Hi");CS0103C# is case-sensitive: Console starts with a capital letter.
Console.WriteLine('Hi');CS1012Single quotes are only for one character (char); text goes in double quotes.
Console.WriteLine("Hi);CS1010The closing quote is missing.
The errors beginners meet most often

Key points

  • C# is a statically typed, object-oriented language by Microsoft; its programs run on the .NET platform.
  • C# is used for websites (ASP.NET Core), games (Unity), desktop and cloud software.
  • Console.WriteLine prints and moves to a new line; Console.Write does not.
  • Every statement ends with ;, and C# is case-sensitive.
  • Dividing two whole numbers gives a whole number: 17 / 5 is 3, and the remainder 17 % 5 is 2.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Which platform do C# programs run on?