csharp-toolkit
Skill1 starsChanged 35 days agoMITView on GitHub
C# / .NET reference — async, xUnit testing, XML docs, DI, SOLID, GoF patterns. Use when writing/reviewing .NET code for quality, patterns, testing, or docs. Routes: this skill = reference material; csharp-dotnet-janitor agent = cleanup/modernization on existing code; code-perfectionist agent = implement + 5-phase validation.
In this repository14 other files it gives its agents.
- appinsights-instrumentationSkill
- delegateSkill
- deploy-verifySkill
- diagnoseSkill
- documentation-writerSkill
- flywheelSkill
- grill-meSkill
- javascript-typescript-jestSkill
- microsoft-docsSkill
- nuget-managerSkill
- performance-optimizationSkill
- python-fastapi-pytestSkill
- sql-toolkitSkill
- tddSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
21 rules. About 0 are common, 11 are distinctive, and 10 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
10
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 24
Use the Async suffix for all async methods
- Very specific: line 26
Consider ValueTask<T> for high-performance scenarios to reduce allocations
- Very specific: line 27
Avoid async void except for event handlers
- Distinctive: line 30
Use try/catch around await expressions
- Very specific: line 31
Use ConfigureAwait(false) in library code to prevent deadlocks
- Very specific: line 32
Use Task.FromException() only in synchronous methods returning Task — in async methods, just throw normally
- Very specific: line 35
Use Task.WhenAll() for parallel execution
- Very specific: line 36
Use Task.WhenAny() for timeouts or first-completed
- Distinctive: line 37
Avoid unnecessary async/await when simply passing through task results
- Distinctive: line 38
Use cancellation tokens for long-running operations
- Very specific: line 41
Never use .Wait(), .Result, or .GetAwaiter().GetResult() in async code
- Distinctive: line 42
Don't mix blocking and async code
- Distinctive: line 43
Always await Task-returning methods
- Very specific: line 44
Use IAsyncEnumerable<T> for async streams
- Distinctive: line 57
Follow AAA pattern (Arrange-Act-Assert)
- Distinctive: line 67
Consider FluentAssertions for more readable assertions
- Distinctive: line 70
Use Moq or NSubstitute alongside xUnit
- Distinctive: line 72
Consider a DI container for complex test setups
- Distinctive: line 145
Read-write: "Gets or sets..."
- Distinctive: line 178
Keep Core projects free of framework dependencies.
- Very specific: line 191
Use Microsoft.Extensions.Logging.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
C# / .NET Toolkit
Reference material for writing and reviewing C# / .NET code. Covers async, testing, XML docs, DI, architecture patterns, and SOLID.
When to use this skill vs. a C#/.NET agent:
| Task | Use |
| Look up correct async pattern, xUnit setup, DI lifetime | this skill |
| Implement a new feature with validation | code-perfectionist agent |
| Modernize old code, remove dead code, fix warnings | csharp-dotnet-janitor agent |
| Review PR for patterns/SOLID violations | this skill + se-security-reviewer agent for security |
Async Programming
Naming & Return Types
- Use the
Asyncsuffix for all async methods - Return
Task<T>when the method returns a value,Taskwhen it doesn't - Consider
ValueTask<T>for high-performance scenarios to reduce allocations - Avoid
async voidexcept for event handlers
Exception Handling
- Use try/catch around await expressions
- Use
ConfigureAwait(false)in library code to prevent deadlocks - Use
Task.FromException()only in synchronous methods returning Task — in async methods, just throw normally
Performance
- Use
Task.WhenAll()for parallel execution - Use
Task.WhenAny()for timeouts or first-completed - Avoid unnecessary async/await when simply passing through task results
- Use cancellation tokens for long-running operations
Pitfalls
- Never use
.Wait(),.Result, or.GetAwaiter().GetResult()in async code - Don't mix blocking and async code
- Always await Task-returning methods
- Use
IAsyncEnumerable<T>for async streams
xUnit Testing
Project Setup
- Separate test project:
[ProjectName].Tests - Reference
Microsoft.NET.Test.Sdk,xunit,xunit.runner.visualstudio - Run with
dotnet test
Test Structure
[Fact]for single tests,[Theory]with[InlineData]/[MemberData]/[ClassData]for data-driven- Follow AAA pattern (Arrange-Act-Assert)
- Name tests:
MethodName_Scenario_ExpectedBehavior - Constructor for setup,
IDisposable.Dispose()for teardown IClassFixture<T>for shared context within a classICollectionFixture<T>for shared context across classes
Assertions
Assert.Equalfor value equality,Assert.Samefor reference equalityAssert.Contains/Assert.DoesNotContainfor collectionsAssert.Throws<T>/await Assert.ThrowsAsync<T>for exceptions- Consider FluentAssertions for more readable assertions
Mocking
- Use Moq or NSubstitute alongside xUnit
- Mock dependencies via interfaces to isolate units under test
- Consider a DI container for complex test setups
Organization
- Group tests by feature or component
[Trait("Category", "CategoryName")]for categorizationITestOutputHelperfor test diagnosticsSkip = "reason"in fact/theory attributes for conditional skipping
Integration Testing with WebApplicationFactory
public class ApiTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;
public ApiTests(WebApplicationFactory<Program> factory)
{
_client = factory.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
services.RemoveAll<DbConnection>();
services.AddScoped<DbConnection>(_ =>
new SqlConnection(TestConstants.TestConnectionString));
});
}).CreateClient();
}
[Fact]
public async Task GetRegistrant_ReturnsOk()
{
var response = await _client.GetAsync("/api/registrants/1");
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
}- One
WebApplicationFactory<Program>per test class viaIClassFixture - Override services in
WithWebHostBuilderto swap external dependencies [Trait("Category", "Integration")]to separate from unit tests- For Dapper: point test
DbConnectionat a test database with known seed data - Wrap each test in a transaction and roll back for isolation
| Scenario | Test type |
| Business logic in a service/handler | Unit test (mock deps) |
| Dapper query returns correct shape | Integration test (real DB) |
| API endpoint returns correct status/body | Integration test (WebApplicationFactory) |
| Input validation rules | Unit test |
XML Documentation
General Rules
<summary>— brief, one-sentence, present-tense third-person verb<remarks>— additional info, implementation details, usage notes<see langword>for keywords (null,true,false)<c>for inline code,<code>for blocks (within<example>)<see cref>for inline references,<seealso>for standalone references<inheritdoc/>to inherit from base classes/interfaces
Methods
<param>— noun phrase, no data type. Start with an article.- Boolean params:
<see langword="true" /> to ...; otherwise, <see langword="false" />. - Out params: "When this method returns, contains ..."
<returns>— noun phrase with article, no data type<typeparam>/<typeparamref>for generic type parameters
Constructors
- Summary: "Initializes a new instance of the <Class> class."
Properties
- Read-write: "Gets or sets..."
- Read-only: "Gets..."
- Boolean: "Gets [or sets] a value that indicates whether..."
<value>— noun phrase. Add default in separate sentence if applicable.
Exceptions
<exception cref>— document all directly thrown exceptions- Describe the condition, not "Thrown if..." — just state the condition directly
Dependency Injection & Services
- Constructor injection is the default. Use primary constructor syntax (
public class MyClass(IDependency dep)) in C# 12+. - Register with appropriate lifetimes: Singleton (stateless/thread-safe), Scoped (per-request), Transient (per-resolution).
- Depend on interfaces (prefix with
I), not concrete types. - Null-check injected deps in the constructor body or use
ArgumentNullException.ThrowIfNull(dep).
Design Patterns
Patterns to prefer
- Command Pattern — generic base classes (
CommandHandler<TOptions>),ICommandHandler<TOptions>interface. - Factory Pattern — complex object creation with service provider integration.
- Repository Pattern — async data access interfaces, provider abstractions for connections.
- Provider Pattern — external service abstractions (DB, AI), clear contracts, configuration handling.
- Resource Pattern —
ResourceManagerfor localized messages, separate.resxfiles.
GoF patterns commonly seen in .NET
Command, Factory, Template Method, Strategy. Verify correct use during review — don't cargo-cult.
Architecture
- Namespace convention:
{Core|Console|App|Service}.{Feature}. - Keep Core projects free of framework dependencies. Console/App projects compose.
- Interface segregation — small focused interfaces beat fat ones.
SOLID
- S Single Responsibility — one reason to change per class.
- O Open/Closed — extend via new code, not by modifying existing.
- L Liskov — subtypes fully substitutable for their base.
- I Interface Segregation — small, client-specific interfaces.
- D Dependency Inversion — depend on abstractions, not concretions.
Error Handling & Logging
- Use
Microsoft.Extensions.Logging. Structured logging with named placeholders (logger.LogInformation("User {UserId} logged in", userId)). - Include scope with
logger.BeginScope(...)for correlated multi-call operations. - Throw specific exception types with descriptive messages. Don't catch
Exceptionunless re-throwing or logging + handling.
Performance & Security
- C# 12+ features and .NET 8+ optimizations where applicable.
- Parameterized queries for all database access (Dapper, EF Core) — never string concat.
- Input validation at API boundaries. Don't trust anything from outside.
Review Checklist
Work through each area when reviewing a PR:
- Patterns — Command / Factory / Provider / Repository correctly implemented? Beneficial patterns missing?
- GoF — Command, Factory, Template Method, Strategy correct?
- SOLID — any principle violations?
- Async — no
.Wait()/.Result, correctConfigureAwait(false)in library code,Task.WhenAllfor parallel? - Performance — proper disposal, pooled resources, parallel opportunities?
- Maintainability — clear SoC, consistent error handling, config done properly?
- Testability — deps behind interfaces, mockable, AAA-compatible?
- Security — input validation, secure credential handling, parameterized queries, safe exception handling?
- Documentation — XML docs for public APIs, params + returns described?
- Clean Code — consistent style, appropriate size, minimal complexity, no duplication?
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

