The snippet library.
Small, practical examples. Search by task or narrow things down below.
Pass behavior as a function delegate
C#: Func describes a callable with typed parameters and a return value.
C#✓ Tested
Perform cleanup even after an exception
C#: A finally block runs after a handled failure as well as after success.
C#✓ Tested
Print a number with two decimal places
C#: Format a small decimal value consistently for display.
C#✓ Tested
Process a FIFO queue
C#: Queue removes elements in the order they were enqueued.
C#✓ Tested
Process a file as a lazy line sequence
C#: ReadLines avoids allocating one string containing the entire file.
C#✓ Tested
Process items by numeric priority
C#: PriorityQueue removes the item with the smallest priority value first.
C#✓ Tested
Provide a fallback for a nullable value
C#: The null-coalescing operator supplies a default only when the left side is null.
C#✓ Tested
Quote a literal search term for a regex
C#: Regex.Escape prevents metacharacters in a literal term from changing the pattern.
C#✓ Tested
Read a big-endian binary integer
C#: Use BinaryPrimitives when the protocol byte order differs from or must not depend on the host.
C#✓ Tested
Read a filename stem and extension
C#: Use Path helpers instead of splitting a path on dots or directory separators.
C#✓ Tested
Read a JSON property
Parse a JSON document and dispose it after reading in C#.
C#✓ Tested
Read a text file
Load a small text file into a C# string.
C#✓ Tested