Introduction to C#: history, features and applications
What is it and why?
Imagine you're building a house from a LEGO set. You have bricks, wheels, windows — these are parts. To assemble a car or a castle, you connect them according to instructions. C# is the same kind of construction set, but for creating programs. You take commands (like bricks) and put them in the correct order so the computer understands what to do.
Why is it needed? C# is one of the most popular languages in the world. It is used to write:
- Games (for example, in Unity — an environment for creating games);
- Applications for Windows (calculators, notepads, browsers);
- Websites (via ASP.NET technology);
- Programs for robots and smart devices.
If you learn C#, you will be able to create your own games, applications, and even make money from it. Also, it is very similar to other languages (Java, C++), so it will be easy to learn new ones later.
Syntax and Code Examples
Syntax is the rules for writing code. Like in the Russian language: "Hello, how are you?" — correct, but "Hello how are you" — with an error. C# also has its own rules. Let's break them down with examples.
Example 1: Hello, World!
This is the first program that all beginners write. It simply outputs text to the screen.
// This is a comment. It is not executed, but simply explains the code.
// Create a class (a box for code) named Program.
class Program
{
// The Main method is the entry point. The program starts working from here.
static void Main()
{
// The Console.WriteLine command outputs text to the console.
Console.WriteLine("Hello, world!"); // Text in quotes.
}
}
Explanation:
class Program— creates a "box" for code. In C#, all code must be inside such boxes.static void Main()— says: "The program starts here." Without this, the computer won't know what to run.Console.WriteLine— the "print string" command. Everything in parentheses will appear on the screen.;— semicolon. In C#, every command ends with this sign. Forget it — you'll get an error.
Example 2: Variables and Math
A variable is a box where you can store numbers, text, or other data. Let's calculate how many apples Petya and Vasya have.
class Program
{
static void Main()
{
// Declare variables. int is an integer.
int applesPetya = 5; // Petya has 5 apples.
int applesVasya = 3; // Vasya has 3 apples.
// Add the numbers.
int totalApples = applesPetya + applesVasya;
// Output the result.
Console.WriteLine("Total apples: " + totalApples); // The + sign concatenates text and a number.
}
}
Explanation:
int— variable type. It says the box will contain an integer (5, -3, 100).=— assignment. Put the value into the box.+— addition. You can add numbers or concatenate text (as in the last line).
Example 3: Conditions (if-else)
The computer can make decisions. For example, if you have 100 rubles, you'll buy pizza, otherwise — a sandwich.
class Program
{
static void Main()
{
int money = 50; // You have 50 rubles.
// Check the condition.
if (money >= 100) // If money is greater than or equal to 100...
{
Console.WriteLine("Buying pizza!"); // ...execute this code.
}
else // Otherwise...
{
Console.WriteLine("Buying a sandwich."); // ...execute this.
}
}
}
Explanation:
if— "if". Write the condition in parentheses (e.g.,money >= 100).else— "otherwise". It triggers if the condition is false.>=— "greater than or equal to". There are also>(greater than),<(less than),==(equal to).
Example 4: Loops (for)
A loop is when a command repeats many times. For example, you want to shout "Hurray!" 5 times.
class Program
{
static void Main()
{
// for loop: (initialization; condition; step)
for (int i = 0; i < 5; i++) // i changes from 0 to 4 (5 times total).
{
Console.WriteLine("Hurray!"); // This line will execute 5 times.
}
}
}
Explanation:
int i = 0— create a counter and set it to 0.i < 5— while the counter is less than 5, execute the code.i++— increase the counter by 1 after each step.
Example 5: Arrays
An array is a list. For example, a list of your grades for the week.
class Program
{
static void Main()
{
// Create an array of 5 numbers.
int[] grades = new int[5]; // [5] — array size.
// Fill the array.
grades[0] = 4; // First grade (index 0).
grades[1] = 5; // Second grade (index 1).
grades[2] = 3;
grades[3] = 4;
grades[4] = 5;
// Calculate the average.
int sum = grades[0] + grades[1] + grades[2] + grades[3] + grades[4];
int average = sum / 5; // Divide the sum by the quantity.
Console.WriteLine("Average grade: " + average);
}
}
Explanation:
int[]— array of integers.new int[5]— allocate space for 5 numbers.grades[0]— access the first element. Indexing starts from 0!
Comparison with Python
Python is another popular language. It is simpler for beginners, but C# provides