FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
C#Getting Started

Find what remains after division

C#: Use the remainder operator to count items left after forming complete groups.

C#✓ Tested
C#LINQ transformations

Flatten nested collections with SelectMany

C#: Concatenate the items from several sequences into one sequence.

C#✓ Tested
C#Data & dates

Format a decimal amount

Use invariant culture to print a C# decimal with two places.

C#✓ Tested
C#JSON & binary formats

Handle malformed JSON

C#: Catch JsonException for malformed JSON without swallowing unrelated failures.

C#✓ Tested
C#Getting Started

Handle three possible outcomes

C#: Check high, normal and low values with an ordered conditional chain.

C#✓ Tested
C#JSON & binary formats

Hash a UTF-8 value with SHA-256

C#: Use SHA256.HashData for a one-shot content digest.

C#✓ Tested
C#Asynchronous work & URI handling

Increment a shared counter atomically

C#: Interlocked performs an indivisible update without a separate lock for this operation.

C#✓ Tested
C#File operations & paths

Inspect a file length without reading it

C#: Use FileInfo.Length to retrieve a logical size in bytes.

C#✓ Tested
C#Asynchronous work & URI handling

Inspect a parsed URI

C#: Use Uri properties instead of splitting URL strings manually.

C#✓ Tested
C#Asynchronous work & URI handling

Limit concurrent access with SemaphoreSlim

C#: Release the semaphore in finally so exceptions cannot leave it permanently acquired.

C#✓ Tested
C#Files & paths

List matching files

Enumerate immediate text files and sort their names in C#.

C#✓ Tested
C#Collections

Look up a dictionary value

Use TryGetValue for a C# dictionary lookup with a fallback.

C#✓ Tested