The snippet library.
Small, practical examples. Search by task or narrow things down below.
Measure a short text value
C#: Read the length of an ASCII string. Non-ASCII text may use multiple code units or bytes depending on the language.
C#✓ Tested
Measure the byte length of UTF-8 text
C#: Character count and encoded byte count can differ for non-ASCII text.
C#✓ Tested
Move a file to a new filename
C#: File.Move changes the file path without loading its contents.
C#✓ Tested
Name a value that stays fixed
C#: Use a constant declaration or the language’s constant naming convention for a fixed limit.
C#✓ Tested
Narrow an object using a type pattern
C#: A successful is pattern introduces a strongly typed local variable.
C#✓ Tested
Normalize canonically equivalent Unicode strings
C#: Normalize to NFC before an ordinal comparison of equivalent character sequences.
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
Pad a numeric label to a fixed width
C#: Use a numeric format specifier to add leading zeroes.
C#✓ Tested
Pair corresponding collection elements
C#: Zip stops at the shorter sequence, so validate lengths separately if mismatches are an error.
C#✓ Tested
Parse a date without a time component
C#: DateOnly avoids attaching an irrelevant time or timezone to a calendar date.
C#✓ Tested
Parse a GUID without an exception
C#: TryParse reports malformed values with a boolean return.
C#✓ Tested
Parse an enum name without throwing
C#: Combine TryParse and IsDefined when only named declared values should be accepted.
C#✓ Tested