C#

C# Cheatsheet

Core C# syntax - types, LINQ queries, and the collection/string methods used constantly.

Basics

var x = 5;

Implicitly-typed local variable

string s = "text";

String type

List<int> list = new();

Generic list

Dictionary<string, int> d = new();

Generic dictionary

public class Foo { }

Class declaration

public int X { get; set; }

Auto-implemented property

LINQ

list.Where(x => x > 0)

Filter a collection

list.Select(x => x * 2)

Transform each element

list.OrderBy(x => x.Name)

Sort a collection

list.FirstOrDefault(x => x.Id == 1)

First match or default value

list.Any(x => x > 0)

True if any element matches

String & null handling

$"{name} is {age}"

String interpolation

string.IsNullOrEmpty(s)

Check for null or empty string

x?.Property

Null-conditional operator

x ?? defaultValue

Null-coalescing operator