Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/C#/Tips & tricks
C#

C#

A productive, strongly typed language for the whole .NET platform.

Paradigm
OOP, functional features
Typing
Static, strong, nullable-aware
Memory
Generational GC
Runs
.NET runtime (JIT) or Native AOT
Concurrency
Tasks, async/await, channels

Tips & tricks

Small idioms with a big payoff.

Switch expressions and pattern matching

Property, positional and relational patterns replace long if/else chains.

string Describe(object shape) => shape switch
{
    Circle { Radius: > 10 } => "large circle",
    Circle c                => 
quot;circle r={c.Radius}", Rectangle(var w, var h) when w == h => "square", null => "nothing", _ => "unknown shape", };

Records with with-expressions

Immutable data types with value equality in one line.

public record Money(decimal Amount, string Currency);

var price = new Money(10m, "CAD");
var discounted = price with { Amount = 8m };   // non-destructive copy
Console.WriteLine(price == new Money(10m, "CAD")); // True: value equality

using declarations

No extra braces; disposal happens at the end of the enclosing scope.

await using var conn = new SqlConnection(cs);   // disposed at end of scope
var users = await conn.QueryAsync<User>("select * from users"); // e.g. Dapper

Saved in this browser only. Part of Clean code foundations (0/5).