360 Secure SkillHub
360 安全技能中心

C#

提供 C# 编码参考,重点说明空引用与 nullable、async/await 死锁、LINQ 延迟执行与多次枚举、值/引用类型与装箱、集合迭代与相等性、IDisposable 等常见陷阱,并给出相关 .NET 实践规则。

ivangdavila
ivangdavila
热度 90.72
最近更新 2026-05-11 15:57:18
分类:开发依赖:纯提示词应用场景:代码生成 / 调试排错 / 概念解释

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add csharp
Metadata
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
  • .Result or .Wait() on UI thread — deadlock, use await or ConfigureAwait(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.Ordinal for code, CurrentCulture for UI
  • GetHashCode() must be stable — mutable fields break dictionary lookup
  • Modifying collection while iterating — throws, use .ToList() to iterate copy
  • decimal for money — float/double have precision loss
  • readonly struct prevents defensive copies — use for performance
  • sealed prevents inheritance — enables devirtualization optimization