C# Programming - Full Guide

01 - Hello World
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}
    

System: Imports system libraries
Main(): Entry point of the program

02 - Variables & Types
int number = 5;
float pi = 3.14f;
bool isCool = true;
string name = "Leah";
char letter = 'A';
    

Basic value types. `int`, `float`, `bool`, `string`, `char`

03 - Operators
int a = 10 + 5;
bool test = a > 3 && a < 20;
a++;
a *= 2;
    

Arithmetic, logical, assignment ops

04 - Control Flow (if, switch)
if (a > 10)
    Console.WriteLine("Big number");

switch (a)
{
    case 5:
        Console.WriteLine("Five");
        break;
    default:
        Console.WriteLine("Something else");
        break;
}
    
05 - Loops
for (int i = 0; i < 5; i++)
    Console.WriteLine(i);

while (a < 10)
    a++;

do
{
    Console.WriteLine("Runs once at least");
} while (false);
    
06 - Arrays & Lists
int[] arr = {1, 2, 3};
List names = new List() {"Alice", "Bob"};

Console.WriteLine(arr[0]);
names.Add("Charlie");
    
07 - Functions
int Add(int x, int y)
{
    return x + y;
}

void Greet(string name)
{
    Console.WriteLine("Hello " + name);
}
    
08 - Classes & Objects
class Dog
{
    public string name;

    public Dog(string n)
    {
        name = n;
    }

    public void Bark()
    {
        Console.WriteLine(name + " says Woof!");
    }
}

Dog myDog = new Dog("Rex");
myDog.Bark();
    
09 - Inheritance & Polymorphism
class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Some sound");
    }
}

class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Meow");
    }
}

Animal a = new Cat();
a.Speak();
    
10 - Interfaces
interface IWeapon
{
    void Fire();
}

class Gun : IWeapon
{
    public void Fire()
    {
        Console.WriteLine("Bang!");
    }
}
    
11 - Enums
enum Direction { Up, Down, Left, Right }

Direction d = Direction.Up;
    
12 - Try/Catch & Exceptions
try
{
    int x = 5 / 0;
}
catch (DivideByZeroException)
{
    Console.WriteLine("Can't divide by zero!");
}
    
13 - File I/O
File.WriteAllText("test.txt", "Hello File");
string text = File.ReadAllText("test.txt");
    

Use `System.IO` namespace

14 - Async & Await
async Task LoadData()
{
    await Task.Delay(1000);
    return 42;
}
    

Async methods return Task or Task<T>

15 - LINQ
int[] nums = {1, 2, 3, 4};
var even = nums.Where(x => x % 2 == 0).ToList();
    

Powerful querying using `System.Linq`

16 - Unity Specific (if you're doing Unity dev)
public class Player : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Game started");
    }

    void Update()
    {
        transform.position += Vector3.forward * Time.deltaTime;
    }
}
    
17 - Attributes
[Serializable]
class PlayerData
{
    public int level;
}
    
18 - Delegates & Events
delegate void MyDelegate();

event MyDelegate OnShitHappened;

void CallEvent()
{
    OnShitHappened?.Invoke();
}
    
19 - Nullable & Null Safety
string? maybeNull = null;

if (maybeNull != null)
    Console.WriteLine(maybeNull.Length);

Console.WriteLine(maybeNull?.Length);
    
20 - Reflection
Type t = typeof(MyClass);
MethodInfo[] methods = t.GetMethods();
    

tagdoesnothing on discord