Pages

Topics

Tools

↑↓navigate↵openescclose

C# guides

Every published C# guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest C# guides

Diagram of shallow copy and deep copy of an object graph in C#.
C#

C# Shallow Copy vs Deep Copy

Understand the difference between shallow and deep copying in C#, how to implement each, and when to choose one over the other.

C#Object CopyingMemberwiseCloneDeep Copy
Diagram showing a value type being boxed into an object reference and later unboxed back to a value type.
C#

C# Object Casting and Boxing: How Type Conversion Works

Understand how C# object casting and boxing work, the runtime cost of boxing, common unboxing errors, and when to prefer generics over object conversions.

C#BoxingType CastingPerformance
Diagram showing a value type being boxed into a heap object with an arrow indicating the allocation overhead.
C#

C# Boxing Performance: Avoiding Hidden Allocations

c# boxing performance: Learn how boxing affects C# performance, where hidden allocations occur, and how generics help you avoid them in hot paths.

boxingunboxinggenericsmemory allocation
Illustration showing a boxed value (object) being opened to reveal the original value type inside, with C# and .NET purple color palette.
C#

Demystifying C# Unboxing

Practical C# unboxing guide: runtime mechanics, InvalidCastException pitfalls, nullable value behavior, and generic alternatives that reduce boxing.

.NETType CastingBoxingPerformance
Diagram showing a value type box being wrapped into a heap object with a reference pointer.
C#

C# Boxing Examples: How Value Types Behave

C# boxing with practical examples: the heap allocation, the exact-type unboxing rule, hidden boxing in APIs and collections, and how generics remove it.

boxingunboxingvalue typesgenerics
Illustration showing a value type boxed into a reference type object and unboxed back, with a warehouse metaphor for heap allocation.
C#

C# Boxing vs Unboxing: How They Affect Performance

Understand how C# boxing and unboxing work, why boxing can add allocations, and how to reduce unnecessary conversions in performance-sensitive code.

BoxingUnboxingPerformanceValue Types
An illustration of sorting cards by a custom rule, showing unordered cards on the left and neatly stacked sorted cards on the right.
C#

C# Custom Sorting with Comparers and LINQ

Learn how to implement custom sorting in C# with IComparer, Comparison delegates, and LINQ OrderBy, including practical examples and performance considerations.

IComparerLINQ OrderBySort()Comparison delegate
Illustration of two lists being sorted and deduplicated using custom comparer logic in C#.
C#

Creating Custom Comparers in C# for Sorting and Equality

Learn how to create custom comparers in C# to control sorting and equality in LINQ, collections, and dictionaries.

IComparerIEqualityComparerComparer<T>Sorting
A diagram showing two objects compared with an equals sign, representing IEquatable<T> equality comparison in C#.
C#

Implementing IEquatable<T> in C# for Efficient Equality Checks

Implement IEquatable<T> in C# to define typed equality checks, avoid boxing for structs, and keep hash-based collections consistent. Includes examples for value types and reference types, plus common pitfalls.

IEquatableEqualityC#GetHashCode
Illustration of a C# object being hashed into a bucket array in a dictionary.
C#

C# GetHashCode: Contract and Correct Overrides

Learn how GetHashCode works in C#, why it matters for Dictionary and HashSet, and how to override it correctly alongside Equals.

GetHashCodeEqualityDictionaryHashSet