FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
C#Types & reliable control flow

Create a modified copy of an immutable record

C#: A with expression creates a shallow copy with selected changed properties.

C#✓ Tested
C#Types & reliable control flow

Narrow an object using a type pattern

C#: A successful is pattern introduces a strongly typed local variable.

C#✓ Tested
C#Types & reliable control flow

Parse an enum name without throwing

C#: Combine TryParse and IsDefined when only named declared values should be accepted.

C#✓ Tested
C#Types & reliable control flow

Pass behavior as a function delegate

C#: Func describes a callable with typed parameters and a return value.

C#✓ Tested
C#Types & reliable control flow

Perform cleanup even after an exception

C#: A finally block runs after a handled failure as well as after success.

C#✓ Tested
C#Types & reliable control flow

Provide a fallback for a nullable value

C#: The null-coalescing operator supplies a default only when the left side is null.

C#✓ Tested
C#Types & reliable control flow

Return multiple named values from a method

C#: Named value tuples give lightweight field names without a separate class.

C#✓ Tested
C#Types & reliable control flow

Use a switch expression to classify a value

C#: Relational patterns express a small ordered range classification.

C#✓ Tested
C#Types & reliable control flow

Validate an argument at a method boundary

C#: ThrowIfNegative gives callers an explicit argument error for an invalid range.

C#✓ Tested
C#Types & reliable control flow

Write a generic first-or-fallback helper

C#: Constrain a generic helper through its input shape and return an explicit caller-supplied fallback.

C#✓ Tested