optimizing-fast-lookup
.NET Fast Lookup
A guide for fast lookup APIs leveraging O(1) time complexity.
Quick Reference: See QUICKREF.md for essential patterns at a glance.
1. Core APIs
| API | Time Complexity | Features |
|---|---|---|
HashSet<T> |
O(1) | Mutable, no duplicates |
FrozenSet<T> |
O(1) | Immutable, .NET 8+ |
Dictionary<K,V> |
O(1) | Mutable, Key-Value |
FrozenDictionary<K,V> |
O(1) | Immutable, .NET 8+ |
2. HashSet
// O(1) time complexity for existence check
var allowedIds = new HashSet<int> { 1, 2, 3, 4, 5 };
if (allowedIds.Contains(userId))
{
// Allowed user
}
// Set operations
setA.IntersectWith(setB); // Intersection
setA.UnionWith(setB); // Union
setA.ExceptWith(setB); // Difference
3. FrozenSet (.NET 8+)
using System.Collections.Frozen;
// Immutable fast lookup (read-only scenarios)
var allowedExtensions = new[] { ".jpg", ".png", ".gif" }
.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
if (allowedExtensions.Contains(fileExtension))
{
// Allowed extension
}
4. Dictionary<K,V> Optimization
// ❌ Two lookups
if (dict.ContainsKey(key))
{
var value = dict[key];
}
// ✅ Single lookup
if (dict.TryGetValue(key, out var value))
{
// Use value
}
// Lookup with default value
var value = dict.GetValueOrDefault(key, defaultValue);
5. Comparer Optimization
// Case-insensitive string comparison
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
set.Add("Hello");
set.Contains("HELLO"); // true
6. When to Use
| Scenario | Recommended Collection |
|---|---|
| Frequently modified set | HashSet<T> |
| Read-only configuration data | FrozenSet<T> |
| Frequent existence checks | HashSet<T> / FrozenSet<T> |
| Key-Value cache | Dictionary<K,V> |
| Static mapping table | FrozenDictionary<K,V> |
7. References
More from christian289/dotnet-with-claudecode
converting-html-css-to-wpf-xaml
Converts HTML/CSS to WPF CustomControl XAML with correct patterns and common pitfall solutions. Use when transforming web designs to WPF, converting CSS animations to Storyboards, implementing CSS border-radius clipping, CSS pseudo-elements (::before/::after), or CSS transforms in XAML.
56publishing-wpf-apps
Guides WPF application publishing and installer options. Use when user mentions publish, deploy, release, packaging, or installer to help choose deployment method and installer technology.
14using-avalonia-collectionview
Provides CollectionView alternatives for AvaloniaUI using DataGridCollectionView and ReactiveUI. Use when filtering, sorting, or grouping collections in AvaloniaUI applications.
9designing-avalonia-customcontrol-architecture
Defines the basic solution structure for AvaloniaUI Desktop Applications using CustomControl. Use when creating new AvaloniaUI projects or designing stand-alone control styles with ControlTheme.
9using-xaml-property-element-syntax
Converts long inline XAML bindings to Property Element Syntax for better readability. Use when XAML binding expressions become too long or complex.
8managing-styles-resourcedictionary
Manages WPF Style definitions and ResourceDictionary patterns including BasedOn inheritance and resource merging. Use when building theme systems, organizing resources, or choosing between StaticResource and DynamicResource.
8