csharp
Language-specific super-code guidelines for csharp.
C#: Idiomatic Efficiency Reference
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for csharp.
Table of Contents
- [LINQ & Collections](#linq)
- [Null Handling](#nulls)
- [Async/Await](#async)
- [Records & Pattern Matching](#records)
- [Error Handling](#errors)
- [Resource Management](#resources)
- [Anti-patterns specific to C#](#antipatterns)
1. LINQ & Collections {#linq}
// ❌ Imperative accumulation
var result = new List<string>();
foreach (var item in items) {
if (item.IsActive) result.Add(item.Name.ToUpper());
}
// ✅
var result = items
.Where(i => i.IsActive)
.Select(i => i.Name.ToUpper())
.ToList();
// ❌ Manual grouping
var grouped = new Dictionary<string, List<Item>>();
foreach (var item in items) {
if (!grouped.ContainsKey(item.Category))
grouped[item.Category] = new List<Item>();
grouped[item.Category].Add(item);
}
// ✅
var grouped = items.GroupBy(i => i.Category)
.ToDictionary(g => g.Key, g => g.ToList());
// ❌ Checking Any() then First()
if (items.Any(i => i.IsValid)) {
var first = items.First(i => i.IsValid);
}
// ✅
var first = items.FirstOrDefault(i => i.IsValid);
if (first is not null) { ... }
Prefer method syntax for chains of 2+ operations. Query syntax is fine for complex joins.
Subscribers only
The full skill, its 1 bundled files and every download is included with every paid Complete AI plan.
Details
| Source | community |
|---|---|
| License | — |
| Risk label | safe ("critical" means the skill may run commands or touch files — read before use) |
| Files | SKILL.md |
| Added | 2026-06-16 |
