C# Cheatsheet

1. Variables and Data Types

int age = 30;
bool isStudent = true;
char grade = 'A';
float salary = 50000.50f;

string name = "John";
int[] numbers = {1, 2, 3};

2. Conditional Statements

if (age > 18) {
  Console.WriteLine("Adult");
} else {
  Console.WriteLine("Minor");
}

switch (grade) {
  case 'A':
    Console.WriteLine("Excellent");
    break;
  case 'B':
    Console.WriteLine("Good");
    break;
  default:
    Console.WriteLine("Average");
}

3. Loops

for (int i = 0; i < 5; i++) {
  Console.WriteLine(i);
}

int i = 0;
while (i < 5) {
  Console.WriteLine(i);
  i++;
}

4. Object-Oriented Programming (OOP)

class Animal {
  public string Name { get; set; }
  public void Speak() {
    Console.WriteLine(Name + " makes a sound.");
  }
}

class Dog : Animal {
  public void Bark() {
    Console.WriteLine(Name + " barks.");
  }
}

Dog rex = new Dog { Name = "Rex" };
rex.Speak();
rex.Bark();

5. LINQ (Language Integrated Query)

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();

evenNumbers.ForEach(n => Console.WriteLine(n));

6. Collections

List<string> fruits = new List<string> { "Apple", "Banana", "Orange" };
fruits.Add("Mango");

foreach (var fruit in fruits) {
  Console.WriteLine(fruit);
}

Dictionary<string, int> scores = new Dictionary<string, int> {
  { "John", 90 },
  { "Jane", 85 }
};

Console.WriteLine(scores["John"]);

7. Delegates and Events

public delegate void Notify();

public class Process {
  public event Notify ProcessCompleted;
  public void StartProcess() {
    Console.WriteLine("Process started.");
    ProcessCompleted?.Invoke();
  }
}

Process process = new Process();
process.ProcessCompleted += () => Console.WriteLine("Process completed!");
process.StartProcess();

8. Exception Handling

try {
  int result = 10 / 0;
} catch (DivideByZeroException e) {
  Console.WriteLine("Cannot divide by zero!");
} finally {
  Console.WriteLine("This runs no matter what.");
}

9. Asynchronous Programming (async/await)

async Task FetchData() {
  await Task.Delay(1000);
  Console.WriteLine("Data fetched after 1 second.");
}

await FetchData();

10. Generics

class Box<T> {
  public T Value { get; set; }
}

Box<int> intBox = new Box<int>();
intBox.Value = 100;

Box<string> strBox = new Box<string>();
strBox.Value = "Hello";

Console.WriteLine(intBox.Value);
Console.WriteLine(strBox.Value);

11. File Handling

using (StreamReader sr = new StreamReader("file.txt")) {
  string line;
  while ((line = sr.ReadLine()) != null) {
    Console.WriteLine(line);
  }
}

12. Anonymous Methods and Lambda Expressions

Action<string> greet = name => Console.WriteLine($"Hello, {name}");
greet("John");

13. Properties

class Person {
  private int age;
  public int Age {
    get { return age; }
    set { age = value > 0 ? value : 0; }
  }
}

Person p = new Person { Age = 25 };
Console.WriteLine(p.Age);

14. Attributes

[Obsolete("This method is obsolete. Use NewMethod() instead.")]
public void OldMethod() {
  Console.WriteLine("Old method");
}

15. Reflection

Type type = typeof(Dog);
Console.WriteLine("Methods of Dog class:");
foreach (var method in type.GetMethods()) {
  Console.WriteLine(method.Name);
}