|
| 1 | +--- |
| 2 | +paths: |
| 3 | + - "**/*.cs" |
| 4 | +--- |
| 5 | + |
| 6 | +# C# Development |
| 7 | + |
| 8 | +- Always use the latest stable C# version available in the project's target framework. |
| 9 | + |
| 10 | +## General Instructions |
| 11 | + |
| 12 | +- Use `Ensure.NotNull(...)` from `CreativeCoders.Core` for null guards |
| 13 | +- Use `Ensure.IsNotNullOrEmpty(...)` from `CreativeCoders.Core` for string guards for arguments that must not be empty |
| 14 | +- Use `Ensure.IsNotNullOrWhitespace(...)` from `CreativeCoders.Core` for string guards for arguments that must not be empty or whitespace |
| 15 | +- Guard arguments for public methods in libraries with `Ensure.NotNull(...)` for all required parameters: |
| 16 | +```csharp |
| 17 | +public void DoSomething(string input, string fileName) |
| 18 | +{ |
| 19 | + Ensure.NotNull(input); |
| 20 | + Ensure.NotNullOrWhitespace(fileName); |
| 21 | + // method implementation |
| 22 | +} |
| 23 | +``` |
| 24 | +- Guard constructor-injected dependencies with `Ensure.NotNull(...)` for all required parameters: |
| 25 | +```csharp |
| 26 | +_service = Ensure.NotNull(service); |
| 27 | +``` |
| 28 | + |
| 29 | +## Formatting |
| 30 | + |
| 31 | +- Apply code-formatting style defined in `.editorconfig`. |
| 32 | +- Prefer file-scoped namespace declarations and single-line using directives. |
| 33 | +- Insert a newline before the opening curly brace of any code block (e.g., after `if`, `for`, `while`, `foreach`, |
| 34 | + `using`, `try`, etc.). |
| 35 | +- Ensure that the final return statement of a method is on its own line. |
| 36 | +- Use `nameof` instead of string literals when referring to member names. |
| 37 | +- Use `[UsedImplicitly]` from JetBrains.Annotations when types are only used via DI or reflection. |
| 38 | +- Use naming conventions from surrounding code when they differ from standard C# conventions. |
| 39 | + |
| 40 | +## Modern C# Features |
| 41 | + |
| 42 | +- Use **primary constructors** when no constructor body is needed. |
| 43 | +- Use private fields with guards instead of using primary constructor parameters directly, unless the parameter is assigned to a property. |
| 44 | + |
| 45 | +## Async/Await |
| 46 | + |
| 47 | +- In **library code** always use `.ConfigureAwait(false)` |
| 48 | +- In **tests** do not use `.ConfigureAwait(false)` (disable for tests via tests/.editorconfig) |
| 49 | +- YOU MUST NOT USE `.GetAwaiter().GetResult()` OR `.Result` OR `.Wait()` TO BLOCK ON ASYNC CODE. If there is no other way ask the user what to do. |
| 50 | + |
| 51 | +## Nullable Reference Types |
| 52 | + |
| 53 | +- Declare variables non-nullable, and check for `null` at entry points. |
| 54 | +- Always use `is null` or `is not null` instead of `== null` or `!= null`. |
| 55 | +- Trust the C# null annotations — don't add null checks when the type system guarantees non-null. |
| 56 | + |
| 57 | +## Documentation |
| 58 | + |
| 59 | +- Document all public members with XML documentation. |
| 60 | +- Use the `csharp-docs` skill to ensure XML documentation follows best practices. |
| 61 | +- If you change code, always update the relevant XML documentation. |
| 62 | + |
| 63 | +## Testing |
| 64 | + |
| 65 | +- Always include test cases for code changes. |
| 66 | +- Always use the `dotnet-tester` skill for writing tests. |
| 67 | + |
| 68 | +## Console |
| 69 | + |
| 70 | +- Use AnsiConsole for console input and output. Always use IAnsiConsole via dependency injection. |
| 71 | +- Use colored output where it makes sense. For example, use green for success messages, red for errors and yellow for warnings. |
| 72 | +- Use tables for structured output when displaying lists of data or multiple pieces of related information. |
| 73 | + |
| 74 | +## Logging |
| 75 | + |
| 76 | +- Use Serilog for logging. |
| 77 | +- Configure Serilog with appropriate sinks (e.g., file, console, Azure Application Insights) based on environment. |
| 78 | +- Always use structured logging with properties for better log analysis and correlation. |
| 79 | + |
| 80 | +## Skills Reference |
| 81 | + |
| 82 | +- Use the `dotnet-aspnet` skill for ASP.NET Core projects (project structure, middleware, auth, validation, error handling, API versioning, OpenAPI). |
| 83 | +- Use the `ef-core` skill for Entity Framework Core data access patterns. |
| 84 | +- Use the `dotnet-sdk-builder` skill for creating .NET SDK/client libraries. |
| 85 | +- Use the `dotnet-reviewer` skill for Reviewing .NET Code. |
| 86 | +- Use the `dotnet-tester` skill for writing tests. |
| 87 | +- Use the `nuget-manager` skill for NuGet package management. |
| 88 | +- Use the `dotnet-inspect` skill to query .NET APIs in NuGet packages, platform libraries (System.*, Microsoft.AspNetCore.*), or local .dll/.nupkg files — discover types and members, diff API surfaces between versions, find extension methods/implementors, locate SourceLink URLs, and triage breakages caused by package upgrades. |
| 89 | +- Use the `csharp-docs` skill to ensure XML documentation follows best practices. |
0 commit comments