csharp — GitHub Copilot rule: what it does, how to install
A rule that pins Copilot to one C# style: naming, nullable types, formatting and ASP.NET Core patterns. Useful when the agent writes differently in every file.
.github/instructions/csharp.instructions.mdWhat csharp does
This is an instruction file Copilot picks up for every **/*.cs file. Inside are concrete requirements: PascalCase for methods and public members, camelCase for private fields, an I prefix on interfaces, is null instead of == null, file-scoped namespaces and single-line using directives.
Beyond style, the rule covers bigger ground: a data access layer on Entity Framework Core, JWT Bearer auth and Microsoft Entra ID, validation with data annotations and FluentValidation, API versioning and Swagger/OpenAPI, structured logging, tests, and container publishing via dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer.
The file goes to .github/instructions/csharp.instructions.md. One catch: the instructions assume C# 14 and ASP.NET Core 10, so on an older target you will be discarding some of the agent's advice by hand. Formatting is delegated to .editorconfig — without that file in the repo, the conventions drift.
What it helps with
- unify class and method naming across a codebase
- replace
== nullchecks withis null - set up a data access layer on EF Core
- add JWT authentication to an ASP.NET Core API
- build a container without hand-writing a Dockerfile
Example prompts for the agent
- Add a new endpoint to the controller following the csharp rules
- Rewrite this service with nullable types,
is nulland file-scoped namespaces - Show how to log a request with Serilog and a correlation ID
How to install csharp
Manually: put the files in place
.github/instructions/csharp.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.claude/rules/csharp.mdThe paths frontmatter field limits the files: move applyTo from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/csharp.mdcFrontmatter: description, globs, alwaysApply. Move applyTo from the source into globs, or the rule will not attach on its own
Manually: put the files in place
.devin/rules/csharp.mdManually: put the files in place
.clinerules/csharp.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about csharp
How do I install csharp in GitHub Copilot?
Create .github/instructions/csharp.instructions.md in your repository and paste the rule contents into it. Copilot applies it to files matching applyTo.
What does the csharp rule do?
It gives Copilot C# conventions: naming, formatting, nullable types, EF Core data access, authentication, validation, logging and tests.
Which C# and ASP.NET versions does the rule target?
The instructions are written for C# 14 and ASP.NET Core 10. On older versions some of the agent's suggestions will not fit.
Source text
csharp.instructions.md · MIT
--- description: 'Guidelines for building C# applications' applyTo: '**/*.cs' --- # C# Development ## C# Instructions - Always use the latest version C#, currently C# 14 features. - Write clear and concise comments for each function. ## General Instructions - Make only high confidence suggestions when reviewing code changes. - Write code with good maintainability practices, including comments on why certain design decisions were made. - Handle edge cases and write clear exception handling. - For libraries or external dependencies, mention their usage and purpose in comments. ## Naming Conventions - Follow PascalCase for component names, method names, and public members. - Use camelCase for private fields and local variables. - Prefix interface names with "I" (e.g., IUserService). ## Formatting - Apply code-formatting style defined in `.editorconfig`. - Prefer file-scoped namespace declarations and single-line using directives. - Insert a newline before the opening curly brace of any code block (e.g., after `if`, `for`, `while`, `foreach`, `using`, `try`, etc.). - Ensure that the final return statement of a method is on its own line. - Use pattern matching and switch expressions wherever possible. - Use `nameof` instead of string literals when referring to member names. - Ensure that XML doc comments are created for any public APIs. When applicable, include `<example>` and `<code>` documentation in the comments. ## Project Setup and Structure - Guide users through creating a new .NET project with the appropriate templates. - Explain the purpose of each generated file and folder to build understanding of the project structure. - Demonstrate how to organize code using feature folders or domain-driven design principles. - Show proper separation of concerns with models, services, and data access layers. - Explain the Program.cs and configuration system in ASP.NET Core 10 including environment-specific settings. ## Nullable Reference Types - Declare variables non-nullable, and check for `null` at entry points. - Always use `is null` or `is not null` instead of `== null` or `!= null`. - Trust the C# null annotations and don't add null checks when the type system says a value cannot be null. ## Data Access Patterns - Guide the implementation of a data access layer using Entity Framework Core. - Explain different options (SQL Server, SQLite, In-Memory) for development and production. - Demonstrate repository pattern implementation and when it's beneficial. - Show how to implement database migrations and data seeding. - Explain efficient query patterns to avoid common performance issues. ## Authentication and Authorization - Guide users through implementing authentication using JWT Bearer tokens. - Explain OAuth 2.0 and OpenID Connect concepts as they relate to ASP.NET Core. - Show how to implement role-based and policy-based authorization. - Demonstrate integration with Microsoft Entra ID (formerly Azure AD). - Explain how to secure both controller-based and Minimal APIs consistently. ## Validation and Error Handling - Guide the implementation of model validation using data annotations and FluentValidation. - Explain the validation pipeline and how to customize validation responses. - Demonstrate a global exception handling strategy using middleware. - Show how to create consistent error responses across the API. - Explain problem details (RFC 9457) implementation for standardized error responses. ## API Versioning and Documentation - Guide users through implementing and explaining API versioning strategies. - Demonstrate Swagger/OpenAPI implementation with proper documentation. - Show how to document endpoints, parameters, responses, and authentication. - Explain versioning in both controller-based and Minimal APIs. - Guide users on creating meaningful API documentation that helps consumers. ## Logging and Monitoring - Guide the implementation of structured logging using Serilog or other providers. - Explain the logging levels and when to use each. - Demonstrate integration with Application Insights for telemetry collection. - Show how to implement custom telemetry and correlation IDs for request tracking. - Explain how to monitor API performance, errors, and usage patterns. ## Testing - Always include test cases for critical paths of the application. - Guide users through creating unit tests. - Do not emit "Act", "Arrange" or "Assert" comments. - Copy existing style in nearby files for test method names and capitalization. - Explain integration testing approaches for API endpoints. - Demonstrate how to mock dependencies for effective testing. - Show how to test authentication and authorization logic. - Explain test-driven development principles as applied to API development. ## Performance Optimization - Guide users on implementing caching strategies (in-memory, distributed, response caching). - Explain asynchronous programming patterns and why they matter for API performance. - Demonstrate pagination, filtering, and sorting for large data sets. - Show how to implement compression and other performance optimizations. - Explain how to measure and benchmark API performance. ## Deployment and DevOps - Guide users through containerizing their API using .NET's built-in container support (`dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer`). - Explain the differences between manual Dockerfile creation and .NET's container publishing features. - Explain CI/CD pipelines for NET applications. - Demonstrate deployment to Azure App Service, Azure Container Apps, or other hosting options. - Show how to implement health checks and readiness probes. - Explain environment-specific configurations for different deployment stages.
© github · MIT · github/awesome-copilot