No history yet

C# Fundamentals

The Building Blocks of Code

At its heart, a program is all about managing information. To do that, we need places to store data. In C#, these storage spots are called variables. Think of a variable as a labeled box where you can keep a specific piece of information, like a number, a piece of text, or a true/false value.

Variable

noun

A named storage location in memory that holds a value. The value can be changed during program execution.

Every variable must have a data type, which tells the computer what kind of information the box can hold. This is important because the type determines how much memory to set aside and what operations are allowed.

Here are some of the most common data types you'll use in C#:

  • int: For whole numbers, like -5, 0, or 100. The name is short for integer.
  • float: For numbers with decimal points, like 3.14 or -0.5. These are also called floating-point numbers.
  • bool: For true or false values. The name comes from Boolean logic.
  • string: For sequences of characters, like "Hello, World!" or a player's name.
// Declaring and initializing variables
int playerScore = 100; // A whole number
float playerHealth = 95.5f; // A decimal number (the 'f' is for float)
bool isAlive = true; // A true or false value
string playerName = "Alex"; // A piece of text

// You can also change a variable's value later
playerScore = 150;

By declaring variables, you create the fundamental pieces of data that your program will manipulate and track.

Making Decisions and Repeating Actions

Programs rarely run in a straight line. They need to make decisions and perform repetitive tasks. This is where control structures come in. They control the flow of your code.

The most basic decision-making tool is the if statement. It checks if a certain condition is true and, if so, runs a specific block of code. You can also add an else block to run code if the condition is false.

Think of an if statement as a fork in the road. If the weather is sunny, you take one path. If not, you take another.

int playerHealth = 75;

if (playerHealth > 50)
{
    // This code runs if health is greater than 50
    Console.WriteLine("Player is in good shape!");
}
else
{
    // This code runs otherwise
    Console.WriteLine("Player needs a health pack!");
}

For repeating actions, we use loops. A for loop is great when you know exactly how many times you want to repeat an action. It has a counter that starts at one value, runs until it hits a limit, and increments with each pass.

// This loop runs 5 times, printing numbers 0 through 4
for (int i = 0; i < 5; i++)
{
    Console.WriteLine("Current number: " + i);
}

A while loop is better when you want to repeat an action as long as a certain condition remains true. It keeps looping until the condition becomes false.

int enemyCount = 3;

while (enemyCount > 0)
{
    Console.WriteLine("An enemy was defeated!");
    enemyCount = enemyCount - 1; // Decrease the count
}

Console.WriteLine("All enemies defeated!");

Creating Reusable Code

Imagine you have a task you need to perform many times throughout your program, like adding two numbers. Instead of writing the same code over and over, you can package it into a reusable block called a method (often called a function in other languages).

A method is a named block of code that performs a specific job. You can give it data to work with (called parameters) and it can return a result back to you.

// This is the method definition
// It takes two integers and returns their sum
int AddNumbers(int num1, int num2)
{
    int result = num1 + num2;
    return result;
}

// Now we can 'call' the method to use it
int sum = AddNumbers(5, 10); // sum will be 15
Console.WriteLine(sum);

Methods make your code cleaner, more organized, and easier to manage. If you need to change how the addition works, you only have to update it in one place: the method itself.

Blueprints for Your Data

As programs get more complex, just using simple variables like int and string isn't enough. We need a way to group related data and behaviors together. This is the central idea behind Object-Oriented Programming (OOP).

In C#, we do this using a class. A class is like a blueprint. It defines a new data type by describing its properties (variables) and its behaviors (methods). For example, we could create a Player class to represent a player in a game.

Class

noun

A blueprint for creating objects. It defines a set of properties and methods that are common to all objects of one type.

// This is the class blueprint
public class Player
{
    // Properties (data)
    public string name;
    public int health;

    // Method (behavior)
    public void TakeDamage(int amount)
    {
        health = health - amount;
        Console.WriteLine(name + " took " + amount + " damage!");
    }
}

Once you have the blueprint (the class), you can create actual instances of it. An instance is called an object. Each object is its own separate entity with its own set of properties.

If a class is the blueprint for a car, an object is an actual car that rolled off the assembly line. You can build many cars from one blueprint.

// Create two objects (instances) from the Player class
Player hero = new Player();
hero.name = "Arin";
hero.health = 100;

Player villain = new Player();
villain.name = "Malak";
villain.health = 150;

// Use the method on one of the objects
hero.TakeDamage(20); // Arin's health is now 80

This approach keeps your code organized by bundling data and the functions that operate on that data together. This concept, called encapsulation, is a pillar of OOP.

Another core concept is inheritance. This allows you to create a new class that is based on an existing class. The new class, called the child or derived class, inherits all the properties and methods of the parent class. You can then add new features or change existing ones. For instance, we could create a Warrior class that inherits from Player but adds a new rage property.

// Warrior inherits from Player
public class Warrior : Player
{
    // It gets name and health automatically
    public int rage; // Add a new property

    public void SwingAxe()
    {
        Console.WriteLine(name + " swings a mighty axe!");
    }
}

Inheritance promotes code reuse and helps build logical relationships between your classes. Finally, polymorphism allows objects of different classes to be treated as objects of a common parent class. It's a powerful concept that adds flexibility, but for now, it's enough to know that it's part of what makes OOP so effective.

Quiz Questions 1/5

Which C# data type would be most appropriate for storing a player's health, which can have decimal values like 98.6?

Quiz Questions 2/5

In Object-Oriented Programming, what is the term for a blueprint that defines the properties and behaviors for a type of object?

These are the absolute fundamentals of C#. With variables, control structures, methods, and classes, you have all the tools you need to start building structured, powerful programs.