C# Cheatsheet - C# Syntax & .NET Reference

This reference is for .NET developers building web APIs, services, or desktop apps, focusing on the C# syntax you write every day: classes and interfaces with their access modifiers, LINQ for querying collections instead of hand-written loops, async/await for I/O-bound work, generics and delegates, and the property/event model. Unlike a flat C# feature list, entries are grouped by the language construct and note where C# behavior differs from Java or JS. After reading you should be able to replace a nested loop with a composed LINQ query, set up an async method without deadlocking, and design a type with properties, events and a delegate.

Languages·39 commands·Last updated 2026-07-21
csharp.NETlinq

Classes & Interfaces 7

public class MyClass { }
Class definition
public interface IMyInterface { void Method(); }
Interface definition
public class Derived : Base, IMyInterface { }
Inherit and implement an interface
public abstract class AbstractClass { }
Abstract class
public sealed class SealedClass { }
Sealed class (not inheritable)
public static class Utils { }
Static class
public record Person(string Name, int Age);
Record type (C# 9)

Properties & Methods 6

public int Age { get; set; }
Auto-property
public string Name { get; init; }
Init-only property (C# 9)
public int Age => _age;
Read-only property (expression-bodied)
public void Method() { }
Method definition
public int Add(int a, int b) => a + b;
Expression-bodied method
public event EventHandler MyEvent;
Event definition

LINQ Queries 8

var result = list.Where(x => x > 5);
Filter
var result = list.Select(x => x * 2);
Map
var result = list.OrderBy(x => x.Age);
Sort
var result = list.GroupBy(x => x.Category);
Group
var result = list.First(x => x.Id == 1);
First match
var result = list.Any(x => x > 0);
Any exists
var result = list.Sum(x => x.Price);
Sum
var result = list.Aggregate((a, b) => a + b);
Aggregate

Asynchronous 6

public async Task<int> GetDataAsync() { }
Async method
await Task.Delay(1000);
Async delay
var result = await httpClient.GetStringAsync(url);
Async HTTP request
Task.Run(() => { });
Run on a background thread
await Task.WhenAll(task1, task2);
Await multiple tasks in parallel
CancellationTokenSource cts = new();
Cancellation token

Generics & Delegates 6

public class Stack<T> { }
Generic class
public T Max<T>(T a, T b) where T : IComparable<T> { }
Generic constraint
public delegate void MyDelegate(string msg);
Delegate definition
public event Action<string> OnMessage;
Action delegate
public Func<int, int, int> add = (a, b) => a + b;
Func delegate
public Predicate<int> isPositive = x => x > 0;
Predicate delegate

Collections & Dictionaries 6

List<int> list = new() { 1, 2, 3 };
List
Dictionary<string, int> dict = new();
Dictionary
HashSet<int> set = new() { 1, 2, 3 };
Hash set
Queue<int> queue = new();
Queue
Stack<int> stack = new();
Stack
var tuple = (1, "hello");
Tuple

Tips

  • LINQ queries are lazily evaluated; they run only when the results are enumerated.
  • async/await is core to asynchronous programming; avoid blocking the UI thread.
  • C# 9 introduced record types, ideal for immutable data models.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us