๐ค
C#
Avoid common C# mistakes โ null traps, async pitfalls, LINQ gotchas, and disposal leaks.
ๅฎๅ
จ้่ฟ
๐ฌPrompt
ๆ่ฝ่ฏดๆ
name: C# slug: csharp version: 1.0.1 description: Write robust C# avoiding null traps, async deadlocks, and LINQ pitfalls. metadata: {"clawdbot":{"emoji":"๐","requires":{"bins":["dotnet"]},"os":["linux","darwin","win32"]}}
Quick Reference
| Topic | File |
|---|---|
| Null reference, nullable types | nulls.md |
| Async/await, deadlocks | async.md |
| Deferred execution, closures | linq.md |
| Value vs reference, boxing | types.md |
| Iteration, equality | collections.md |
| IDisposable, using, finalizers | dispose.md |
Critical Rules
?.and??prevent NRE but!overrides warnings โ still crashes if null.Resultor.Wait()on UI thread โ deadlock, useawaitorConfigureAwait(false)- LINQ is lazy โ
query.Where(...)doesn't execute until iteration - Multiple enumeration of IEnumerable โ may re-query database, call
.ToList()first - Closure captures variable, not value โ loop variable in lambda captures last value
- Struct in async method โ copied, modifications lost after await
- String comparison culture โ
StringComparison.Ordinalfor code,CurrentCulturefor UI GetHashCode()must be stable โ mutable fields break dictionary lookup- Modifying collection while iterating โ throws, use
.ToList()to iterate copy decimalfor money โfloat/doublehave precision lossreadonly structprevents defensive copies โ use for performancesealedprevents inheritance โ enables devirtualization optimization
ๅฆไฝไฝฟ็จใC#ใ๏ผ
- ๆๅผๅฐ้พ่พAI๏ผWeb ๆ iOS App๏ผ
- ็นๅปไธๆนใ็ซๅณไฝฟ็จใๆ้ฎ๏ผๆๅจๅฏน่ฏๆกไธญ่พๅ ฅไปปๅกๆ่ฟฐ
- ๅฐ้พ่พAI ไผ่ชๅจๅน้ ๅนถ่ฐ็จใC#ใๆ่ฝๅฎๆไปปๅก
- ็ปๆๅณๆถๅ็ฐ๏ผๆฏๆ็ปง็ปญๅฏน่ฏไผๅ