int x = 10; | Integer (32-bit) |
long l = 10L; | Long (64-bit) |
double d = 3.14; | Double (64-bit) |
decimal m = 3.14m; | Decimal (128-bit) |
bool b = true; | Boolean |
char c = 'A'; | Character (16-bit) |
string s = "hello"; | String |
var x = 10; | Implicit type |
const int MAX = 100; | Constant |
int? nullable = null; | Nullable value type |
nullable ?? 0 | Null coalescing |
nullable?.Property | Null conditional |
nullable ??= default; | Null coalescing assignment |
object!.Method() | Null forgiving |
Nullable.GetValueOrDefault() | Get value or default |
int[] arr = new int[5]; | Array declaration |
int[] arr = { 1, 2, 3 }; | Array initialization |
int[,] matrix = new int[3, 3]; | 2D array |
int[][] jagged = new int[3][]; | Jagged array |
arr.Length | Array length |
Array.Sort(arr) | Sort array |
arr[^1] | Index from end |
arr[1..3] | Range operator |
public class MyClass { } | Class definition |
public MyClass() { } | Constructor |
public MyClass(int x) : this() | Constructor chaining |
~MyClass() { } | Finalizer |
public int Property { get; set; } | Auto property |
public int Property { get; init; } | Init-only property |
public required string Name { get; set; } | Required property (C# 11) |
class Child : Parent | Inherit from class |
class MyClass : IInterface | Implement interface |
base.Method() | Call base method |
virtual void Method() | Virtual method |
override void Method() | Override method |
sealed class MyClass | Sealed class |
abstract class MyClass | Abstract class |
interface IMyInterface { } | Interface definition |
record Person(string Name, int Age); | Record type |
record class Person { } | Record class |
record struct Point(int X, int Y); | Record struct |
struct MyStruct { } | Struct definition |
readonly struct Point { } | Readonly struct |
person with { Name = "New" } | With expression |
List<T> | Dynamic array |
Dictionary<K, V> | Key-value dictionary |
HashSet<T> | Unique set |
Queue<T> | FIFO queue |
Stack<T> | LIFO stack |
LinkedList<T> | Linked list |
SortedSet<T> | Sorted unique set |
SortedDictionary<K, V> | Sorted dictionary |
list.Add(item) | Add item |
list.Remove(item) | Remove item |
list.Contains(item) | Check contains |
list.Find(x => x > 0) | Find by predicate |
list.FindAll(x => x > 0) | Find all by predicate |
list.Sort() | Sort list |
list.ForEach(x => Console.WriteLine(x)) | For each item |
dict.TryGetValue(key, out var value) | Try get value |
from x in collection | From clause |
where x > 0 | Where filter |
orderby x.Name | Order by |
orderby x descending | Order descending |
select x.Property | Select projection |
select new { x.Name, x.Age } | Anonymous type |
group x by x.Category | Group by |
join y in col2 on x.Id equals y.Id | Join |
.Where(x => x > 0) | Filter |
.Select(x => x.Name) | Project |
.OrderBy(x => x.Name) | Order ascending |
.OrderByDescending(x => x) | Order descending |
.GroupBy(x => x.Category) | Group by |
.Join(col2, x => x.Id, y => y.Id, (x, y) => ...) | Join collections |
.SelectMany(x => x.Items) | Flatten nested |
.Distinct() | Remove duplicates |
.Count() | Count elements |
.Sum(x => x.Value) | Sum values |
.Average(x => x.Value) | Average |
.Min() / .Max() | Min/Max |
.First() / .FirstOrDefault() | First element |
.Single() / .SingleOrDefault() | Single element |
.Any(x => x > 0) | Any match |
.All(x => x > 0) | All match |
async Task<T> MethodAsync() | Async method |
await task | Await task |
await Task.WhenAll(tasks) | Wait for all |
await Task.WhenAny(tasks) | Wait for any |
Task.Run(() => { }) | Run on thread pool |
Task.Delay(1000) | Async delay |
.ConfigureAwait(false) | No sync context |
CancellationToken token | Cancellation token |
async IAsyncEnumerable<T> Method() | Async enumerable |
yield return item; | Yield return |
await foreach (var x in stream) | Await foreach |
[EnumeratorCancellation] CancellationToken token | Cancellation support |
is int n | Type pattern |
is { Name: "test" } | Property pattern |
is (int x, int y) | Positional pattern |
is > 0 | Relational pattern |
is > 0 and < 10 | Logical pattern |
is not null | Negation pattern |
is [1, 2, ..] | List pattern (C# 11) |
x switch { pattern => result } | Switch expression |
_ => defaultValue | Discard pattern |
int n when n > 0 => ... | When clause |
case int n: break; | Case pattern |