The snippet library.
Small, practical examples. Search by task or narrow things down below.
Sum integer values
Sum a C# sequence using LINQ.
C#✓ Tested
Take a page from an ordered sequence
C#: Apply a stable order before Skip and Take for predictable paging.
C#✓ Tested
Test whether any or all values match
C#: Any and All short-circuit instead of materializing filtered collections.
C#✓ Tested
Transform each item with Select
C#: Project each input value into a new value without modifying the source array.
C#✓ Tested
Trim surrounding whitespace
Remove whitespace at the ends of a C# string.
C#✓ Tested
Try parsing an integer
Use TryParse to avoid exceptions for invalid C# integer input.
C#✓ Tested
Update a JSON document with mutable nodes
C#: JsonNode permits targeted changes before serializing a document again.
C#✓ Tested
Update a running count
C#: Change a variable after its initial value has been assigned.
C#✓ Tested
Use a last-in first-out stack
C#: Stack is useful for reversible traversal and other nested operations.
C#✓ Tested
Use a switch expression to classify a value
C#: Relational patterns express a small ordered range classification.
C#✓ Tested
Use case-insensitive dictionary keys
C#: Choose an ordinal comparer when case-insensitive keys represent identifiers.
C#✓ Tested
Validate a complete identifier with a regex
C#: Use absolute anchors and a timeout so the regex checks the complete input with a bounded execution time.
C#✓ Tested