SuperAPI AI Directory

csharp

Coding & development

View repository

Updated 10-15 20:31

About this skill

C#

Write idiomatic, nullable and explicit C#. Types model the domain; async does not hide I/O. Web: aspnet-core. Persistence: entity-framework. Tests: csharp-testing.

Tooling

  • .NET 10+ SDK in new code; respect TargetFramework / global.json of the repo.
  • Nullable reference types enabled (<Nullable>enable</Nullable>).
  • dotnet format, dotnet test, analyzers (TreatWarningsAsErrors in new code if CI already requires it).
  • Central Package Management (Directory.Packages.props) if the repo already uses it.

Organization

src/
  MyApp/                 # exe ou worker
  MyApp.Domain/
  MyApp.Infrastructure/
tests/
  MyApp.Tests/
  • Projects by boundary, not by cosmetic layer.
  • internal by default; public only stable API.
  • File-scoped namespaces; implicit usings only the SDK ones.

Language

  • Records / record struct for values; classes for identity and lifecycle.
  • Primary constructors when the type is thin; do not hide too many mutable fields.
  • Pattern matching (is, switch expressions) instead of if + cast.
  • required / init for contract objects; avoid public setters without an invariant.
  • Collections: List<T>, IReadOnlyList<T>, FrozenDictionary when immutable and hot.
  • LINQ for clarity; avoid N+1 in I/O (materialize / batch).
  • span/Memory only in a measured hot path.
public sealed record UserId(Guid Value);

public static string Label(UserStatus status) => status switch
{
    UserStatus.Active => "Ativo",
    UserStatus.Suspended => "Suspenso",
    _ => throw new ArgumentOutOfRangeException(nameof(status)),
};

Nullability and errors

  • T? explicit; no blind ! . Null-forgiving only with a commented local invariant.
  • Lib APIs: Result<T> / domain exceptions — choose one per boundary.
  • ArgumentNullException.ThrowIfNull; validate at the edge.
  • Do not catch (Exception) swallowing. Preserve InnerException.

Async

  • async Task/ValueTask to the edge; do not .Result / .Wait().
  • Pass CancellationToken in I/O. Cancel on shutdown.
  • ConfigureAwait(false) in libs; ASP.NET apps do not need it.
  • Heavy CPU: Task.Run only with reason; do not wrap sync blocking.

DI and config (apps)

  • Register lifetimes consciously (Singleton stateless, Scoped per request/UoW).
  • Options pattern + IValidateOptions; secrets out of code.
  • No service locator (IServiceProvider.GetRequiredService in the domain).

Anti-patterns

  • dynamic / object in new APIs
  • async void (except UI event handlers)
  • Assume HttpContext / DbContext in this skill
  • DateTime.Now in the domain — inject a clock
  • Static "helper" classes with state

Integration with other skills

  • HTTP: aspnet-core
  • ORM: entity-framework
  • Tests: csharp-testing
  • Realtime: signalr
  • HTTP contracts: api-design

Completion criteria

  • Nullable and warnings handled in scope
  • Async with CT at I/O boundaries
  • Minimal public API; records/enums in the domain
  • Tests of the critical path (csharp-testing)
  • No .Result / Exception swallowing