- 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.
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:
Console.WriteLine("Hello, world!");Hello, world!
Consoleis a ready-made class for working with the console (the text window).WriteLineis 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.
namespace HelloApp;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}Hello, world!
Main method.How do you run a program?
- 1Install 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.
- 2Create a project
In a terminal, type
dotnet new console -o HelloApp. AProgram.csfile appears in theHelloAppfolder. - 3Run it
Go to the folder with
cd HelloAppand typedotnet run. The program is compiled andHello, World!appears on the screen. - 4Or just one file
Starting with .NET 10 you can even skip the project: put your code into an
app.csfile and rundotnet run app.cs.
dotnet new console -o HelloApp
cd HelloApp
dotnet runText 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.
Console.Write("Salam, ");
Console.Write("Aysel!");
Console.WriteLine();
Console.WriteLine("I live in Baku.");
Console.WriteLine("I am " + 12 + " years old.");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.
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));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.
// 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!");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.
| Code | Error | Why |
|---|---|---|
Console.WriteLine("Hi") | CS1002: ; expected | The semicolon at the end is missing. |
console.WriteLine("Hi"); | CS0103 | C# is case-sensitive: Console starts with a capital letter. |
Console.WriteLine('Hi'); | CS1012 | Single quotes are only for one character (char); text goes in double quotes. |
Console.WriteLine("Hi); | CS1010 | The closing quote is missing. |
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.WriteLineprints and moves to a new line;Console.Writedoes not.- Every statement ends with
;, and C# is case-sensitive. - Dividing two whole numbers gives a whole number:
17 / 5is3, and the remainder17 % 5is2.
Check yourself
10 questions. Every correct answer earns XP.