🔷 C#

First Hello World program — C#

What is this and why?

Imagine you're learning to play a new computer game. The first thing you do is press the "New Game" button and see the splash screen. In programming, this is called "Hello World" — the simplest program that just prints a greeting. Why is this needed? Just like in any game you first need to learn to walk, and then to run, so in C# you first need to learn to run a program and output text. This is like the first step into the world of programming: you learn to say "Hello!" to the computer, and it answers you.

When you write your first program, you'll understand the main thing: a computer is not a smart robot, but simply an executor of commands. It does exactly what you tell it, and nothing else. "Hello World" is your first conversation with the computer in the C# language.

Syntax and code examples

Syntax is the rules of a programming language, just like in the Russian language there are rules: "zhi-shi write with the letter i". C# also has its own rules. Let's look at the simplest program:

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

Let's break it down piece by piece:

  • using System; — this is like including a library. Imagine you want to draw a picture, but you don't have paints. "using System" is like saying: "Give me a box of paints".
  • class Program — this is a container for your code. Like a box for toys.
  • static void Main() — this is the entry point. When the program starts, it looks for this exact place and begins executing commands.
  • Console.WriteLine(...) — a command that outputs text to the screen.

Example 2: Outputting multiple lines

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, world!");
        Console.WriteLine("My name is C#");
        Console.WriteLine("I am learning to program");
    }
}

Each Console.WriteLine command outputs a new line. Note: after each command there is a semicolon ; — this is like a period at the end of a sentence. If you forget it, the computer won't understand where the command ends.

Example 3: Outputting numbers and text together

using System;
class Program
{
    static void Main()
    {
        int age = 12;
        Console.WriteLine("I am " + age + " years old");
    }
}

Here we created a variable age and put the number 12 into it. Then we combined the text and the number using the + sign. This is called concatenation — joining strings together.

Example 4: Using variables

using System;
class Program
{
    static void Main()
    {
        string name = "Anna";
        int year = 2024;
        Console.WriteLine("Hello, " + name + "!");
        Console.WriteLine("It is currently the year " + year);
    }
}

A variable is like a box with a label. The box is labeled "name", and inside it is "Anna". The string type is for text, int is for whole numbers.

Example 5: Simple math

using System;
class Program
{
    static void Main()
    {
        int a = 5;
        int b = 3;
        int sum = a + b;
        Console.WriteLine("5 + 3 = " + sum);
    }
}

The computer can add numbers. We created two variables, added them, and output the result. Note: sum is also a variable, into which we put the result of the addition.

Comparison with Python

Python is another programming language that is often taught in schools. Let's see how the same "Hello World" program looks in Python:

print("Hello, World!")

See the difference? In Python, it's just one line, but in C# it's a whole 7 lines. Why is that? C# is like a heavy truck: it's powerful, but requires more preparation. Python is like a bicycle: quick to learn, but with fewer capabilities for large projects.

Let's compare the example with variables:

name = "Anna"
year = 2024
print("Hello, " + name + "!")
print("It is currently the year " + str(year))

In Python, you don't need to write string or int — it figures out the data type itself. But in C#, you must explicitly state: "This is text", "This is a number". It's like labeling boxes: in Python, the boxes are transparent, but in C#, they are opaque, but you know exactly what's inside.

The main difference: C# requires more code, but it is stricter and clearer for large programs. Python is simpler to start with, but it's easier to make a mistake that is hard to find.

Typical beginner mistakes

Here are the most common mistakes that beginner C# programmers make:

  • Forgot the semicolon — the most popular mistake. Every command must end with ;. If you forget, the computer will show a red error.
  • Incorrect quotation marks — in C#, only straight quotation marks " are used, not «» or ''. Text is always in double quotes.
  • Error in command spelling — Console.WriteLine is written with capital C and W. If you write console.writeline, the computer won't understand.
  • Forgot curly braces — after class Program and static void Main(), curly braces { } are mandatory. They are like cabinet doors: you open them, put the code inside, and close them.
  • Confusion with case sensitivity — C# distinguishes between uppercase and lowercase letters. Main and main are different things. The computer looks specifically for Main with a capital letter.
  • Extra spaces — spaces in C# are usually