No history yet

Introduction to C#

The Language of C#

C# (pronounced “C sharp”) is a programming language, which is just a formal way to give instructions to a computer. Think of it like a recipe. Each step has to be written in a language the chef—in this case, the computer—understands. C# is popular for building games, web apps, and desktop software.

Every C# program has a specific structure. Instructions are grouped together inside curly braces {}, and most lines end with a semicolon ;. This tells the computer where one instruction ends and the next begins. Let's look at the simplest C# program, which just prints a message to the screen.

using System;

class Program
{
    static void Main()
    {
        // This line prints the message to the console.
        Console.WriteLine("Hello, World!");
    }
}

Let's break that down:

  • using System; includes a basic library of commands.
  • class Program is a container for our code.
  • static void Main() is the entry point. When you run the program, the computer looks for Main and starts executing the instructions inside its curly braces.
  • Console.WriteLine(...) is the command that prints text to the console, and the text itself goes inside the parentheses and quotes.

Storing Information

To do anything useful, a program needs to remember information. It does this using variables. A variable is like a labeled box where you can store a piece of data. You give the box a name and tell the computer what type of data you plan to put inside.

C# has several built-in data types for different kinds of information. Here are the most common ones you'll use.

Data TypeDescriptionExample
intStores whole numbers (integers).10, -5, 0
floatStores numbers with decimal points.3.14f, -0.5f
stringStores text, like names or messages."Alex", "Hello!"
boolStores a true or false value.true, false

To create a variable, you declare its type, give it a name, and then assign it a value using the equals sign =. This process is called initialization.

// An integer for a player's score.
int score = 100;

// A float for health points. Notice the 'f' at the end.
float health = 95.5f;

// A string for the player's name.
string playerName = "Alex";

// A boolean to track if the game is over.
bool isGameOver = false;

When you create a float, you need to add an f at the end of the number. This tells C# to treat it as a floating-point number and not a different decimal type like a double.

Making Decisions

Programs often need to make choices. What if you want to show a special message only if the player's score is high enough? You can do this with conditional statements. The most common is the if statement, which runs a block of code only if a certain condition is true.

You can expand on this with else if to check for other conditions, and else to provide a default action if none of the conditions are met.

int score = 85;

if (score > 90)
{
    Console.WriteLine("Excellent work!");
}
else if (score > 70)
{
    Console.WriteLine("Good job.");
}
else
{
    Console.WriteLine("You can do better next time.");
}

In this example, the program first checks if score is greater than 90. It's not, so it skips that block. Then it checks if score is greater than 70. It is, so it prints "Good job." and ignores the final else block completely.

Repeating Actions

Sometimes you need to repeat an action multiple times. Instead of copying and pasting code, you can use a loop. Loops run a block of code over and over until a certain condition is met.

A for loop is great when you know exactly how many times you want to repeat something. It has three parts: an initializer (where it starts), a condition (how long it runs), and an iterator (how it changes each time).

// This loop runs 5 times.
for (int i = 0; i < 5; i++)
{
    Console.WriteLine("Counting: " + i);
}

This loop creates a temporary integer i and sets it to 0. It will continue running as long as i is less than 5. After each loop, i++ increases the value of i by one. The output will be the numbers 0 through 4 printed on separate lines.

A while loop is better when you don't know how many repetitions you need. It simply continues as long as its condition is true. For example, you might want to keep a game running as long as the player is alive.

int health = 100;

while (health > 0)
{
    Console.WriteLine("Player is still in the game!");
    // In a real game, something would happen here to decrease health.
    health = health - 10;
}

This loop will print its message as long as the health variable is greater than 0. Each time the loop runs, we subtract 10 from health, eventually causing the condition to become false and the loop to stop.

And that's a quick look at the building blocks of C#. With variables to store data, conditionals to make decisions, and loops to repeat actions, you have the core tools to start writing simple but powerful programs.