0 day streak
Code library

Intermediate C# exercises

Read these Intermediate C# snippets line by line — each one comes with a written breakdown of what the code does and why.

C# · Intermediate

Filtering Collections with LINQ

LINQ (Language Integrated Query) provides a powerful, declarative way to manipulate collections. The intermediate level involves chaining multiple operators to transform data efficiently.

SyntaxBest Practices
Open snippet →
C# · Intermediate

Asynchronous Programming with Tasks

Using async/await allows the application to remain responsive during I/O-bound operations by yielding control back to the caller while waiting for the task to complete.

Async & ConcurrencyPerformance
Open snippet →
C# · Intermediate

Immutability with C# Records

Records are reference types that provide built-in functionality for encapsulating data. They use value-based equality and support non-destructive mutation via the 'with' expression.

Data TypesOOP
Open snippet →
C# · Intermediate

Exception Filtering with 'when'

Exception filters allow you to catch an exception only when a specific condition is met. This is cleaner than catching and re-throwing, as it doesn't unwind the stack unnecessarily.

Error HandlingBest Practices
Open snippet →
C# · Intermediate

Deferred Execution with 'yield return'

The 'yield return' statement indicates that the method is an iterator. It provides a value to the enumerator without generating the entire collection in memory at once, enabling efficient processin…

Control FlowPerformance
Open snippet →
C# · Intermediate

Encapsulating Logic with Local Functions

Local functions allow you to define methods inside the scope of another method. This is useful for helper logic that should not be accessible by other members of the class, promoting cleaner encaps…

FunctionsSyntax
Open snippet →
C# · Intermediate

Building a Custom Middleware Pipeline

Middleware patterns allow you to chain behaviors around a core logic. This implementation uses delegates and higher-order functions to wrap a handler, a common architectural pattern found in modern…

Design PatternsFrameworks
Open snippet →
C# · Intermediate

Decoupling Logic for Testability

Using interfaces allows for dependency injection, which is critical for unit testing. By depending on IEmailService rather than a concrete class, you can easily inject a mock implementation during…

TestingOOP
Open snippet →
C# · Intermediate

Cryptographically Secure Hashing with PBKDF2

PBKDF2 (Password-Based Key Derivation Function 2) is a standard for secure password hashing. It applies a pseudo-random function to the password along with a salt many times, making brute-force att…

SecurityBest Practices
Open snippet →
C# · Intermediate

High-Performance Memory Management with Span

Span<T> represents a contiguous region of memory. It allows for high-performance operations like slicing arrays without creating expensive copies. It is especially useful in performance-critical ap…

Arrays & ListsPerformanceMemory Management
Open snippet →
C# · Intermediate

Implementing the Standard Disposal Pattern

The IDisposable pattern is a fundamental pattern for managing memory and unmanaged resources. It ensures that resources are released deterministically rather than waiting for the Garbage Collector…

Memory ManagementDesign Patterns
Open snippet →
C# · Intermediate

Generic Array Mapping without Linq

Understanding how to manually implement generic transformations on arrays helps master both Generics and Function delegates. This approach avoids overhead from external libraries and focuses on cor…

Arrays & ListsOOP
Open snippet →
C# · Intermediate

Advanced Property Pattern Matching

Property patterns allow you to match an object's properties against specific values or ranges. This is highly readable and replaces complex nested if-statements.

Design PatternsSyntaxControl Flow
Open snippet →
C# · Intermediate

Initializing and Accessing Jagged Arrays

Jagged arrays are arrays of arrays where each row can have a different length. This is more memory-efficient than a multidimensional array (rectangular) when the data is sparse or uneven.

Arrays & ListsMemory ManagementData Types
Open snippet →
C# · Intermediate

Object Classification via Positional Patterns

Positional patterns allow you to deconstruct an object and match against its parts. This is highly effective for state-based logic and replaces long if-else chains with readable switch expressions.

Design PatternsControl FlowSyntax
Open snippet →