The snippet library.
Small, practical examples. Search by task or narrow things down below.
Await an asynchronous method result
C#: Use Task<T> for an async operation with a typed result.
C#✓ Tested
Await multiple tasks while preserving result order
C#: Task.WhenAll returns results in the same order as the supplied tasks.
C#✓ Tested
Escape a URL query value
C#: EscapeDataString percent-encodes one value rather than an entire URL.
C#✓ Tested
Escape text for an HTML context
C#: HtmlEncode converts markup delimiters in plain text before rendering it as HTML content.
C#✓ Tested
Increment a shared counter atomically
C#: Interlocked performs an indivisible update without a separate lock for this operation.
C#✓ Tested
Inspect a parsed URI
C#: Use Uri properties instead of splitting URL strings manually.
C#✓ Tested
Limit concurrent access with SemaphoreSlim
C#: Release the semaphore in finally so exceptions cannot leave it permanently acquired.
C#✓ Tested
Observe cancellation through a token
C#: A cancellation token is cooperative; operations must check it or accept it as an argument.
C#✓ Tested
Parse a GUID without an exception
C#: TryParse reports malformed values with a boolean return.
C#✓ Tested
Resolve a relative URI against a base
C#: Use a base URI ending in a slash when the final path component represents a directory.
C#✓ Tested