1. Program Structure & Output
C#
using System; // Imports the System library for basic tasks

namespace MyFirstApp {
    class Program {
        static void Main(string[] args) {
            // This is the starting point of every C# console program
            Console.WriteLine("Hello World!"); // Prints with a new line
            Console.Write("Stays on same line."); 
        }
    }
}
2. Variables & Data Types
C#
// Basic Types
int age = 25;                       // Whole numbers
double pi = 3.14159;                // Decimal numbers (standard)
decimal price = 19.99m;             // High precision (use 'm' for money!)
float speed = 5.5f;                 // Low precision decimal (use 'f')
char initial = 'A';                 // Single character (single quotes)
string message = "Hello";           // Text (double quotes)
bool isCoding = true;               // Logic (true/false)

// Modern Helpers
var autoType = "I am a string";     // 'var' lets C# figure out the type
const int gravity = 9;              // 'const' cannot be changed later
3. Input & String Handling
C#
Console.Write("Enter name: ");
string input = Console.ReadLine();   // Reads text from user

// String Interpolation (The best way to join strings)
string bio = $"User {input} is {age} years old."; 

// Useful String Methods
input.ToUpper();                    // "HELLO"
input.ToLower();                    // "hello"
input.Length;                       // Number of characters
input.Contains("abc");              // Returns true/false
4. Arithmetic & Comparison
C#
int sum = 10 + 5;                   // 15 (Addition)
int diff = 10 - 5;                  // 5  (Subtraction)
int prod = 10 * 5;                  // 50 (Multiplication)
int quot = 10 / 3;                  // 3  (Integer division drops remainder)
int rem = 10 % 3;                   // 1  (Modulo/Remainder)

// Comparisons (Return Booleans)
bool isEqual = (10 == 10);          // true
bool isNotEqual = (10 != 5);        // true
bool isGreater = (10 > 5);          // true
5. Control Flow (Logic)
C#
// If/Else
if (age >= 18) {
    Console.WriteLine("Adult");
} else if (age > 12) {
    Console.WriteLine("Teen");
} else {
    Console.WriteLine("Child");
}

// Switch Statement (Cleaner than many Ifs)
string day = "Monday";
switch (day) {
    case "Monday": Console.WriteLine("Back to work!"); break;
    case "Friday": Console.WriteLine("Almost weekend!"); break;
    default: Console.WriteLine("Just another day."); break;
}
6. Loops (Repetition)
C#
// For Loop (Repeat X times)
for (int i = 0; i < 5; i++) {
    Console.WriteLine($"Count: {i}");
}

// While Loop (Repeat while condition is true)
while (age < 30) {
    age++;
}

// Foreach Loop (Best for Lists/Arrays)
string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits) {
    Console.WriteLine(fruit);
}
7. Collections (Arrays & Lists)
C#
// Arrays (Fixed size)
int[] numbers = new int[3] { 1, 2, 3 };

// Lists (Flexible size - Requires: using System.Collections.Generic;)
List<string> names = new List<string>();
names.Add("Alice");                 // Add item
names.Remove("Alice");              // Remove item
int count = names.Count;            // Get size
8. Methods (Functions)
C#
// Access / ReturnType / Name / (Parameters)
public static int Multiply(int a, int b) 
{
    return a * b;
}

// Void means it returns nothing
public static void Greet() 
{
    Console.WriteLine("Hi!");
}
9. Classes & Object Oriented Programming (OOP)
C#
class Dog 
{
    // Fields
    public string Breed;
    
    // Properties (Get/Set logic)
    public int Age { get; set; } 

    // Constructor (Runs when object is created)
    public Dog(string breed) {
        Breed = breed;
    }

    // Method
    public void Bark() {
        Console.WriteLine("Woof!");
    }
}

// Usage:
Dog myDog = new Dog("Golden Retriever");
myDog.Age = 5;
myDog.Bark();
10. Error Handling (Try/Catch)
C#
try {
    int result = 10 / 0; // Will crash
} catch (Exception ex) {
    Console.WriteLine("Error: " + ex.Message);
} finally {
    Console.WriteLine("This always runs.");
}