C# Basics

Master the fundamental concepts of C# programming with our comprehensive beginner's guide.

Lesson 1 Beginner

Variables & Data Types

Learn how to store and manipulate data using variables and understand C#'s basic data types.

Learn Now
Lesson 2 Beginner

Operators

Master arithmetic, comparison, logical, and assignment operators to perform operations on your data.

Learn Now
Lesson 3 Beginner

Control Flow

Control the execution of your program using conditional statements (if/else) and loops (for/while).

Learn Now
Lesson 4 Beginner

Methods

Organize your code into reusable blocks using methods with parameters and return values.

Learn Now

Lesson 1: Variables & Data Types

Variables are containers for storing data values. In C#, you must declare the type of a variable when you declare it.

Basic Data Types:

DataTypes.cs
// Integer types
int age = 25;              // Whole numbers
long bigNumber = 10000000000L; // Large whole numbers

// Floating point types
float price = 19.99f;        // Single precision
double pi = 3.1415926535;   // Double precision

// Boolean
bool isCSharpFun = true;    // true or false

// Character
char grade = 'A';           // Single character

// String
string name = "Code Crusader"; // Text

// Var keyword (type inference)
var message = "Hello World"; // Compiler determines type

Pro Tip

Use meaningful variable names like userAge instead of ua to make your code more readable.

Try It Yourself:

Practice.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        // Declare variables for a user profile
        string userName = "Crusader123";
        int userAge = 28;
        double accountBalance = 1250.75;
        bool isPremiumMember = true;

        // Display the information
        Console.WriteLine($"Username: {userName}");
        Console.WriteLine($"Age: {userAge}");
        Console.WriteLine($"Account Balance: ${accountBalance}");
        Console.WriteLine($"Premium Member: {isPremiumMember}");
    }
}

Exercise

Create a program that stores information about a book (title, author, year published, and price) and displays it.

Lesson 2: Operators

Operators perform operations on variables and values. C# has several types of operators for different purposes.

Operator Types:

Operators.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        // Arithmetic operators
        int sum = 10 + 5;    // +, -, *, /, %
        int power = (int)Math.Pow(2, 3); // 2^3 = 8

        // Assignment operators
        int x = 10;
        x += 5; // x = x + 5

        // Comparison operators
        bool isEqual = (10 == 5); // ==, !=, >, <, >=, <=

        // Logical operators
        bool result = (10 > 5) && (20 < 30); // && (AND), || (OR), ! (NOT)

        // Ternary operator
        string message = (x > 10) ? "Greater than 10" : "10 or less";
    }
}

Try It Yourself:

Calculator.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        Console.Write("Enter first number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());
        
        Console.Write("Enter second number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());
        
        Console.WriteLine($"Addition: {num1 + num2}");
        Console.WriteLine($"Subtraction: {num1 - num2}");
        Console.WriteLine($"Multiplication: {num1 * num2}");
        Console.WriteLine($"Division: {num1 / num2}");
        Console.WriteLine($"Modulus: {num1 % num2}");
    }
}

Exercise

Create a program that checks if a number is even or odd using the modulus operator.

Lesson 3: Control Flow

Control flow statements let you decide which blocks of code to execute and how many times.

Conditional Statements:

Conditionals.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        // If statement
        int age = 18;
        
        if (age >= 18) {
            Console.WriteLine("You are an adult");
        }
        
        // If-else statement
        int temperature = 25;
        
        if (temperature > 30) {
            Console.WriteLine("It's hot outside");
        }
        else if (temperature > 20) {
            Console.WriteLine("It's warm outside");
        }
        else {
            Console.WriteLine("It's cold outside");
        }
        
        // Switch statement
        string day = "Monday";
        
        switch (day) {
            case "Monday":
                Console.WriteLine("Start of the work week");
                break;
            case "Friday":
                Console.WriteLine("Almost weekend!");
                break;
            default:
                Console.WriteLine("Regular day");
                break;
        }
    }
}

Loops:

Loops.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        // For loop
        for (int i = 1; i <= 5; i++) {
            Console.WriteLine($"Count: {i}");
        }
        
        // While loop
        int count = 0;
        while (count < 3) {
            Console.WriteLine($"While loop iteration: {count}");
            count++;
        }
        
        // Do-while loop
        int x = 5;
        do {
            Console.WriteLine($"Do-while value: {x}");
            x--;
        } while (x > 0);
        
        // Foreach loop
        string[] colors = { "Red", "Green", "Blue" };
        foreach (string color in colors) {
            Console.WriteLine($"Color: {color}");
        }
    }
}

Exercise

Create a program that prints numbers from 1 to 100, but for multiples of 3 print "Fizz", for multiples of 5 print "Buzz", and for multiples of both print "FizzBuzz".

Lesson 4: Methods

Methods are blocks of code that perform specific tasks and can be reused throughout your program.

Creating and Using Methods:

Methods.cs
using System;

class Program
{
    // Simple method without parameters or return value
    static void Greet() {
        Console.WriteLine("Hello, Crusader!");
    }
    
    // Method with parameters
    static void GreetUser(string name) {
        Console.WriteLine($"Hello, {name}!");
    }
    
    // Method with return value
    static int Add(int a, int b) {
        return a + b;
    }
    
    // Method with optional parameter
    static void DisplayInfo(string name, int age = 18) {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
    
    // Main method - entry point of the program
    static void Main(string[] args)
    {
        // Calling methods
        Greet();
        GreetUser("Alice");
        
        int result = Add(5, 7);
        Console.WriteLine($"5 + 7 = {result}");
        
        DisplayInfo("Bob");
        DisplayInfo("Charlie", 25);
    }
}

Try It Yourself:

CalculatorMethods.cs
using System;

class Calculator
{
    static double Add(double a, double b) {
        return a + b;
    }
    
    static double Subtract(double a, double b) {
        return a - b;
    }
    
    static double Multiply(double a, double b) {
        return a * b;
    }
    
    static double Divide(double a, double b) {
        if (b == 0) {
            Console.WriteLine("Error: Division by zero");
            return 0;
        }
        return a / b;
    }
    
    static void Main(string[] args)
    {
        Console.WriteLine("Simple Calculator");
        Console.WriteLine("1. Add");
        Console.WriteLine("2. Subtract");
        Console.WriteLine("3. Multiply");
        Console.WriteLine("4. Divide");
        
        Console.Write("Enter choice (1-4): ");
        int choice = Convert.ToInt32(Console.ReadLine());
        
        Console.Write("Enter first number: ");
        double num1 = Convert.ToDouble(Console.ReadLine());
        
        Console.Write("Enter second number: ");
        double num2 = Convert.ToDouble(Console.ReadLine());
        
        double result = 0;
        
        switch (choice) {
            case 1:
                result = Add(num1, num2);
                break;
            case 2:
                result = Subtract(num1, num2);
                break;
            case 3:
                result = Multiply(num1, num2);
                break;
            case 4:
                result = Divide(num1, num2);
                break;
            default:
                Console.WriteLine("Invalid choice");
                break;
        }
        
        Console.WriteLine($"Result: {result}");
    }
}

Exercise

Create a program with methods to calculate the area of different shapes (circle, rectangle, triangle) and display the results.