Skip to main content
rule official · GitHub

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.

Install into GitHub Copilot · Manually: put the files in place All methods and platforms ↓
.github/instructions/csharp.instructions.md
Open on GitHub

What 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 == null checks with is 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 null and 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.md

The applyTo field sets which files it applies to

Step 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

Similar IDE rules

All IDE rules for Programming languages →

From the same set732

The whole set →
dataverse-python-error-handling Guidance for handling Dataverse SDK for Python exceptions, inspecting response codes, and deciding which failed requests are worth retrying. ★ 40,418repo stars dataverse-python-best-practices A GitHub Copilot rule with working patterns for the Dataverse SDK for Python: auth, client setup, CRUD operations. ★ 40,418repo stars dataverse-python-agentic-workflows A GitHub Copilot rule covering agentic workflows with the Dataverse SDK for Python: autonomous agents, MCP, A2A and data quality checks. ★ 40,418repo stars dart-n-flutter This rule guides Dart and Flutter work in .dart files, covering code style, documentation, language usage, and API design. ★ 40,418repo stars csharp-razorpages Configures GitHub Copilot to write idiomatic Razor Pages across .cshtml and .cshtml.cs files. It enforces PageModel conventions, secure model binding, and clean data queries. ★ 40,418repo stars csharp-mcp-server This rule guides C# MCP server work with the Model Context Protocol SDK. It fits projects where tools, prompts, DI, and stdio transport need a consistent setup. ★ 40,418repo stars cpp-language-service-tools This rule guides GitHub Copilot to use C++ IntelliSense tools for definitions, references, and call relationships. It helps when refactoring or changing function signatures. ★ 40,418repo stars copilot-thought-logging This rule makes Copilot keep a Copilot-Processing.md file with a plan and progress marks, so you can watch the run and steer it. ★ 40,418repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor