FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
C#Text validation & encoding

Check an extension without case sensitivity

C#: Use an explicit ordinal case-insensitive comparison for a known filename suffix.

C#✓ Tested
C#Getting Started

Choose between two branches

C#: Use if and else to handle an available or empty stock count.

C#✓ Tested
C#Dates & precise numbers

Clamp a number to inclusive bounds

C#: Math.Clamp returns the nearest endpoint when the value lies outside the range.

C#✓ Tested
C#Collections & data structures

Combine collections as a distinct set

C#: Union retains each value once across both inputs.

C#✓ Tested
C#Getting Started

Combine text with a name

C#: Construct a greeting from a fixed prefix and a stored name.

C#✓ Tested
C#Getting Started

Compare a value with a limit

C#: Turn a comparison into a readable yes or no result.

C#✓ Tested
C#Dates & precise numbers

Convert a timestamp to a different fixed offset

C#: ToOffset changes the displayed local time while retaining the same instant.

C#✓ Tested
C#JSON & binary formats

Convert bytes to Base64 and back

C#: Encode text to UTF-8 before converting it to a Base64 representation.

C#✓ Tested
C#Getting Started

Convert known numeric text to an integer

C#: Convert a trusted literal to an integer before adding. Validate external input before converting it.

C#✓ Tested
C#Dates & precise numbers

Convert Unix seconds to an offset-aware timestamp

C#: DateTimeOffset preserves the UTC offset explicitly.

C#✓ Tested
C#File operations & paths

Copy a file without overwriting

C#: File.Copy defaults to refusing a destination that already exists.

C#✓ Tested
C#Getting Started

Count the items in a collection

C#: Read the collection’s length instead of hard-coding the loop limit.

C#✓ Tested