0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
C# · IntermediateSnippet

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.

snippet.cs
csharp
1
2
3
4
5
var activeUsers = users
.Where(u => u.IsActive)
.OrderBy(u => u.LastName)
.Select(u => u.Email)
.ToList();
SyntaxBest Practices
C# · IntermediateSnippet

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.

snippet.cs
csharp
1
2
3
4
5
6
public async Task<string> DownloadDataAsync(string url)
{
using var client = new HttpClient();
string result = await client.GetStringAsync(url);
return result.Trim();
}
Async & ConcurrencyPerformance
C# · IntermediateSnippet

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.

snippet.cs
csharp
1
2
3
4
public record Product(string Id, string Name, decimal Price);
var original = new Product("A1", "Laptop", 999.99m);
var discount = original with { Price = 899.99m };
Data TypesOOP
C# · IntermediateSnippet

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.

snippet.cs
csharp
1
2
3
4
5
6
7
8
try
{
PerformNetworkAction();
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
HandleMissingResource();
}
Error HandlingBest Practices
C# · IntermediateSnippet

Pattern Matching Switch Expressions

Switch expressions provide a more concise syntax for pattern matching. They allow testing object properties directly and returning a value based on the first match.

snippet.cs
csharp
1
2
3
4
5
6
7
string priority = task switch
{
{ IsCompleted: true } => "None",
{ IsUrgent: true, DaysLeft: < 2 } => "Critical",
{ IsUrgent: true } => "High",
_ => "Standard"
};
SyntaxControl Flow
C# · IntermediateSnippet

Deterministic Resource Cleanup with IDisposable

The IDisposable interface is used to provide a mechanism for releasing unmanaged resources, such as file handles or database connections, which the Garbage Collector cannot handle automatically.

snippet.cs
csharp
1
2
3
4
5
6
7
8
public class Logger : IDisposable
{
private StreamWriter _writer = new StreamWriter("log.txt");
public void Dispose()
{
_writer?.Dispose();
}
}
Memory ManagementBest Practices
C# · IntermediateSnippet

Extending Existing Types with Extension Methods

Extension methods allow you to 'add' methods to existing types without creating a new derived type or modifying the original type. They are defined as static methods in a static class.

snippet.cs
csharp
1
2
3
4
5
6
7
public static class StringExtensions
{
public static int WordCount(this string str)
{
return str.Split(new[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
SyntaxBest Practices
C# · IntermediateSnippet

Accessing Objects via Custom Indexers

Indexers allow instances of a class or struct to be indexed just like arrays. This is useful for classes that represent a collection of data or provide access to internal buffers.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
public class SimpleCache
{
private string[] _data = new string[10];
public string this[int index]
{
get => _data[index];
set => _data[index] = value;
}
}
OOPSyntax