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

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
C#JSON & binary formats

Measure the byte length of UTF-8 text

C#: Character count and encoded byte count can differ for non-ASCII text.

C#✓ Tested
C#File operations & paths

Move a file to a new filename

C#: File.Move changes the file path without loading its contents.

C#✓ Tested
C#Getting Started

Name a value that stays fixed

C#: Use a constant declaration or the language’s constant naming convention for a fixed limit.

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#Text validation & encoding

Normalize canonically equivalent Unicode strings

C#: Normalize to NFC before an ordinal comparison of equivalent character sequences.

C#✓ Tested
C#Asynchronous work & URI handling

Observe cancellation through a token

C#: A cancellation token is cooperative; operations must check it or accept it as an argument.

C#✓ Tested
C#Text validation & encoding

Pad a numeric label to a fixed width

C#: Use a numeric format specifier to add leading zeroes.

C#✓ Tested
C#LINQ transformations

Pair corresponding collection elements

C#: Zip stops at the shorter sequence, so validate lengths separately if mismatches are an error.

C#✓ Tested
C#Dates & precise numbers

Parse a date without a time component

C#: DateOnly avoids attaching an irrelevant time or timezone to a calendar date.

C#✓ Tested
C#Asynchronous work & URI handling

Parse a GUID without an exception

C#: TryParse reports malformed values with a boolean return.

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