C# Cheat Sheet
C# is a modern, strongly typed programming language designed for building desktop, web, cloud, and cross-platform applications on the .NET ecosystem. This C# cheatsheet provides a structured, example-driven reference to core language features, syntax, object-oriented constructs, LINQ, and asynchronous programming.
Compilation & Execution
dotnet new console
dotnet run
csc Program.cs
Program.exe
Minimal Program
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C#");
}
}
Console.WriteLine("Hello, C#");
Built-in Types
int a = 10;
double b = 3.14;
float c = 1.5f;
bool d = true;
char e = 'A';
string f = "text";
sizeof(int);
sizeof(double);
Nullable Types
int? value = null;
if (value.HasValue)
{
Console.WriteLine(value.Value);
}
int result = value ?? 0;
Variables & Type Inference
var x = 10;
var y = "text";
dynamic z = 5;
z = "now string";
Control Flow
if / else
if (x > 10)
{
result = 1;
}
else
{
result = 0;
}
switch
switch (x)
{
case 1:
break;
case 2:
break;
default:
break;
}
var label = x switch
{
1 => "one",
2 => "two",
_ => "other"
};
Loops
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
while (x > 0)
{
x--;
}
foreach (var item in collection)
{
Console.WriteLine(item);
}
Methods
static int Add(int a, int b)
{
return a + b;
}
int result = Add(2, 3);
Method Overloading
static int Add(int a, int b);
static double Add(double a, double b);
Optional & Named Parameters
static int Multiply(int a, int b = 2)
{
return a * b;
}
Multiply(5);
Multiply(b: 3, a: 4);
Classes
class Counter
{
private int value;
public Counter()
{
value = 0;
}
public void Increment()
{
value++;
}
public int Get()
{
return value;
}
}
Properties
class User
{
public string Name { get; set; }
public int Age { get; private set; }
public void SetAge(int age)
{
Age = age;
}
}
Structs
struct Point
{
public int X;
public int Y;
}
Point p = new Point { X = 10, Y = 20 };
Records
record Person(string Name, int Age);
var person = new Person("Name", 30);
Inheritance
class Animal
{
public void Speak() { }
}
class Dog : Animal
{
}
Polymorphism & Virtual Methods
class Base
{
public virtual void Run() { }
}
class Derived : Base
{
public override void Run() { }
}
Interfaces
interface ILogger
{
void Log(string message);
}
class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
Enums
enum Status
{
Ok,
Error,
Pending
}
Arrays
int[] numbers = { 1, 2, 3 };
numbers[0] = 10;
Collections
List
using System.Collections.Generic;
List<int> list = new List<int> { 1, 2, 3 };
list.Add(4);
Dictionary
Dictionary<int, string> map = new Dictionary<int, string>();
map[1] = "one";
LINQ
using System.Linq;
var evens = numbers.Where(n => n % 2 == 0).ToList();
var names = users.Select(u => u.Name);
var grouped = users.GroupBy(u => u.Age);
var result =
from n in numbers
where n > 5
select n;
Delegates
delegate int Operation(int a, int b);
Operation op = Add;
int r = op(2, 3);
Func & Action
Func<int, int, int> sum = (a, b) => a + b;
Action<string> log = message => Console.WriteLine(message);
Events
class Notifier
{
public event Action OnNotify;
public void Notify()
{
OnNotify?.Invoke();
}
}
Exception Handling
try
{
int x = int.Parse("10");
}
catch (FormatException)
{
}
finally
{
}
Async / Await
using System.Threading.Tasks;
async Task<int> GetValueAsync()
{
await Task.Delay(100);
return 42;
}
int value = await GetValueAsync();
Tasks
Task task = Task.Run(() =>
{
Console.WriteLine("running");
});
await task;
File I/O
using System.IO;
File.WriteAllText("file.txt", "content");
string text = File.ReadAllText("file.txt");
Pattern Matching
if (obj is int number)
{
Console.WriteLine(number);
}
var result = obj switch
{
int n => n * 2,
string s => s.Length,
_ => 0
};
Value vs Reference Types
int a = 10;
int b = a;
b = 20;
class Box { public int Value; }
Box x = new Box { Value = 10 };
Box y = x;
y.Value = 20;
Span (Overview)
Span<int> span = stackalloc int[3] { 1, 2, 3 };