一键导入
the-standard-code-csharp
Enforces the C# naming, organization, method, variable, class, field, instantiation, and comment/documentation rules of The Standard.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Enforces the C# naming, organization, method, variable, class, field, instantiation, and comment/documentation rules of The Standard.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | The Standard Code CSharp |
| description | Enforces the C# naming, organization, method, variable, class, field, instantiation, and comment/documentation rules of The Standard. |
| the standard version | v2.13.0 |
| skill version | v0.3.0.0 |
This skill is the canonical C# coding-style and organization layer for The Standard. Use it whenever generating, reviewing, refactoring, or evaluating C# code.
This skill explicitly covers all supplied C# style rule files:
It also preserves the implementation-profile naming conventions supplied in the implementation specification.
Use this skill for any C# or .NET code, including models, brokers, services, controllers, tests, and support code.
this. for private fields.The following are the naming conventions and guidance for naming C# files.
File names should follow the PascalCase convention followed by the file extension .cs.
Student.cs
StudentService.cs
student.cs
studentService.cs
Student_Service.cs
Partial class files are files that contain nested classes for a root file. For instance:
Both validations and exceptions are partial classes to display a different aspect of any given class in a multi-dimensional space.
StudentService.Validations.cs
StudentService.Validations.Add.cs
StudentServiceValidations.cs
StudentService_Validations.cs
Variable names should be concise and representative of nature and the quantity of the value it holds or will potentially hold.
var student = new Student();
var s = new Student();
var stdnt = new Student();
The same rule applies to lambda expressions:
students.Where(student => student ... );
students.Where(s => s ... );
var students = new List<Student>();
var studentList = new List<Student>();
var student = new Student();
var studentModel = new Student();
var studentObj = new Student();
If a variable value is it's default such as 0 for int or null for strings and you are not planning on changing that value (for testing purposes for instance) then the name should identify that value.
Student noStudent = null;
Student student = null;
int noChangeCount = 0;
int changeCount = 0;
Declaring a variable and instantiating it should indicate the immediate type of the variable, even if the value is to be determined later.
If the right side type is clear, then use var to declare your variable
var student = new Student();
Student student = new Student();
If the right side isn't clear (but known) of the returned value type, then you must explicitly declare your variable with it's type.
Student student = GetStudent();
var student = GetStudent();
If the right side isn't clear and unknown (such as an anonymous types) of the returned value type, you may use var as your variable type.
var student = new
{
Name = "Hassan",
Score = 100
};
Assign properties directly if you are declaring a type with one property.
var inputStudentEvent = new StudentEvent();
inputStudentEvent.Student = inputProcessedStudent;
var inputStudentEvent = new StudentEvent
{
Student = inputProcessedStudent
};
var studentEvent = new StudentEvent
{
Student = someStudent,
Date = someDate
}
var studentEvent = new StudentEvent();
studentEvent.Student = someStudent;
studentEvent.Date = someDate;
If a variable declaration exceeds 120 characters, break it down starting from the equal sign.
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
List<Student> washgintonSchoolsStudentsWithGrades = await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
Declarations that occupy two lines or more should have a new line before and after them to separate them from previous and next variables declarations.
Student student = GetStudent();
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
School school = await GetSchoolAsync();
Student student = GetStudent();
List<Student> washgintonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
School school = await GetSchoolAsync();
Also, declarations of variables that are of only one line should have no new lines between them.
Student student = GetStudent();
School school = await GetSchoolAsync();
Student student = GetStudent();
School school = await GetSchoolAsync();
Method names should be a summary of what the method is doing, it needs to stay percise and short and representative of the operation with respect to synchrony.
Method names must contain verbs in them to represent the action it performs.
public List<Student> GetStudents()
{
...
}
public List<Student> Students()
{
...
}
Asynchronous methods should be postfixed by the term Async such as methods returning Task or ValueTask in general.
public async ValueTask<List<Student>> GetStudentsAsync()
{
...
}
public async ValueTask<List<Student>> GetStudents()
{
...
}
Input parameters should be explicit about what property of an object they will be assigned to, or will be used for any action such as search.
public async ValueTask<Student> GetStudentByNameAsync(string studentName)
{
...
}
public async ValueTask<Student> GetStudentByNameAsync(string text)
{
...
}
public async ValueTask<Student> GetStudentByNameAsync(string name)
{
...
}
If your method is performing an action with a particular parameter specify it.
public async ValueTask<Student> GetStudentByIdAsync(Guid studentId)
{
...
}
public async ValueTask<Student> GetStudentAsync(Guid studentId)
{
...
}
When utilizing a method, if the input parameters aliases match the passed in variables in part or in full, then you don't have to use the aliases, otherwise you must specify your values with aliases.
Assume you have a method:
Student GetStudentByNameAsync(string studentName);
string studentName = "Todd";
Student student = await GetStudentByNameAsync(studentName);
Student student = await GetStudentByNameAsync(studentName: "Todd");
Student student = await GetStudentByNameAsync(toddName);
Student student = await GetStudentByNameAsync("Todd");
Student student = await GetStudentByNameAsync(todd);
In general encapsulate multiple lines of the same logic into their own method, and keep your method at level 0 of details at all times.
Any method that contains only one line of code should use fat arrows
public List<Student> GetStudents() => this.storageBroker.GetStudents();
public List<Student> Students()
{
return this.storageBroker.GetStudents();
}
If a one-liner method exceeds the length of 120 characters then break after the fat arrow with an extra tab for the new line.
public async ValueTask<List<Student>> GetAllWashingtonSchoolsStudentsAsync() =>
await this.storageBroker.GetStudentsAsync();
public async ValueTask<List<Student>> GetAllWashingtonSchoolsStudentsAsync() => await this.storageBroker.GetStudentsAsync();
If a method contains multiple liners separated or connected via chaining it must have a scope. Unless the parameters are going on the next line then a one-liner method with multi-liner params is allowed.
public Student AddStudent(Student student)
{
ValidateStudent(student);
return this.storageBroker.InsertStudent(student);
}
public Student AddStudent(Student student)
{
return this.storageBroker.InsertStudent(student)
.WithLogging();
}
public Student AddStudent(
Student student)
{
return this.storageBroker.InsertStudent(student);
}
public Student AddStudent(Student student) =>
this.storageBroker.InsertStudent(student)
.WithLogging();
public Student AddStudent(
Student student) =>
this.storageBroker.InsertStudent(student);
For multi-liner methods, take a new line between the method logic and the final return line (if any).
public List<Student> GetStudents()
{
StudentsClient studentsApiClient = InitializeStudentApiClient();
return studentsApiClient.GetStudents();
}
public List<Student> GetStudents()
{
StudentsClient studentsApiClient = InitializeStudentApiClient();
return studentsApiClient.GetStudents();
}
With mutliple method calls, if both calls are less than 120 characters then they may stack unless the final call is a method return, otherwise separate with a new line.
public List<Student> GetStudents()
{
StudentsClient studentsApiClient = InitializeStudentApiClient();
List<Student> students = studentsApiClient.GetStudents();
return students;
}
public List<Student> GetStudents()
{
StudentsClient studentsApiClient = InitializeStudentApiClient();
List<Student> students = studentsApiClient.GetStudents();
return students;
}
public async ValueTask<List<Student>> GetStudentsAsync()
{
StudentsClient washingtonSchoolsStudentsApiClient =
await InitializeWashingtonSchoolsStudentsApiClientAsync();
List<Student> students = studentsApiClient.GetStudents();
return students;
}
public async ValueTask<List<Student>> GetStudentsAsync()
{
StudentsClient washingtonSchoolsStudentsApiClient =
await InitializeWashingtonSchoolsStudentsApiClientAsync();
List<Student> students = studentsApiClient.GetStudents();
return students;
}
A method declaration should not be longer than 120 characters.
public async ValueTask<List<Student>> GetAllRegisteredWashgintonSchoolsStudentsAsync(
StudentsQuery studentsQuery)
{
...
}
public async ValueTask<List<Student>> GetAllRegisteredWashgintonSchoolsStudentsAsync(StudentsQuery studentsQuery)
{
...
}
If you are passing multiple parameters, and the length of the method call is over 120 characters, you must break by the parameters, with one parameter on each line.
List<Student> redmondHighStudents = await QueryAllWashingtonStudentsByScoreAndSchoolAsync(
MinimumScore: 130,
SchoolName: "Redmond High");
List<Student> redmondHighStudents = await QueryAllWashingtonStudentsByScoreAndSchoolAsync(
MinimumScore: 130,SchoolName: "Redmond High");
Some methods offer extensions to call other methods. For instance, you can call a Select() method after a Where() method. And so on until a full query is completed.
We will follow a process of Uglification Beautification. We uglify our code to beautify our view of a chain methods. Here's some examples:
students.Where(student => student.Name is "Elbek")
.Select(student => student.Name)
.ToList();
students
.Where(student => student.Name is "Elbek")
.Select(student => student.Name)
.ToList();
The first approach enforces simplifying and cutting the chaining short as more calls continues to uglify the code like this:
students.SomeMethod(...)
.SomeOtherMethod(...)
.SomeOtherMethod(...)
.SomeOtherMethod(...)
.SomeOtherMethod(...);
The uglification process forces breaking down the chains to smaller lists then processing it. The second approach (no uglification approach) may require additional cognitive resources to distinguish between a new statement and an existing one as follows:
student
.Where(student => student.Name is "Elbek")
.Select(student => student.Name)
.OrderBy(student => student.Name)
.ToList();
ProcessStudents(students);
Classes that represent services or brokers in a Standard-Compliant architecture should represent the type of class in their naming convention, however that doesn't apply to models.
class Student {
...
}
class StudentModel {
}
In a singular fashion, for any class that contains business logic.
class StudentService {
....
}
class StudentsService{
...
}
class StudentBusinessLogic {
...
}
class StudentBL {
...
}
In a singular fashion, for any class that is a shim between your services and external resources.
class StudentBroker {
....
}
class StudentsBroker {
...
}
In a plural fashion, to reflect endpoints such as /api/students to expose your logic via RESTful operations.
class StudentsController {
....
}
class StudentController {
...
}
A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
Class fields are named in a camel cased fashion.
class StudentsController {
private readonly string studentName;
}
class StudentController {
private readonly string StudentName;
}
class StudentController {
private readonly string _studentName;
}
Should follow the same rules for naming as mentioned in the Variables sections.
When referencing a class private field, use this keyword to distinguish private class member from a scoped method or constructor level variable.
class StudentsController {
private readonly string studentName;
public StudentsController(string studentName) {
this.studentName = studentName;
}
}
class StudentController {
private readonly string _studentName;
public StudentsController(string studentName) {
_studentName = studentName;
}
}
If the input variables names match to input aliases, then use them, otherwise you must use the aliases, especially with values passed in.
int score = 150;
string name = "Josh";
var student = new Student(name, score);
var student = new Student(name: "Josh", score: 150);
var student = new Student("Josh", 150);
Student student = new (...);
When instantiating a class instance - make sure that your property assignment matches the properties order in the class declarations.
public class Student
{
public Guid Id {get; set;}
public string Name {get; set;}
}
var student = new Student
{
Id = Guid.NewGuid(),
Name = "Elbek"
}
public class Student
{
private readonly Guid id;
private readonly string name;
public Student(Guid id, string name)
{
this.id = id;
this.name = name;
}
}
var student = new Student (id: Guid.NewGuid(), name: "Elbek");
public class Student
{
public Guid Id {get; set;}
public string Name {get; set;}
}
var student = new Student
{
Name = "Elbek",
Id = Guid.NewGuid()
}
public class Student
{
private readonly Guid id;
private readonly string name;
public Student(string name, Guid id)
{
this.id = id;
this.name = name;
}
}
var student = new Student (id: Guid.NewGuid(), name: "Elbek");
public class Student
{
private readonly Guid id;
private readonly string name;
public Student(Guid id, string name)
{
this.id = id;
this.name = name;
}
}
var student = new Student (name: "Elbek", id: Guid.NewGuid());
Comments can only be used to explain what code can't. Whether the code is visible or not.
Comments highlighting copyrights should follow this pattern:
// ---------------------------------------------------------------
// Copyright (c) Coalition of the Good-Hearted Engineers
// FREE TO USE TO CONNECT THE WORLD
// ---------------------------------------------------------------
//----------------------------------------------------------------
// <copyright file="StudentService.cs" company="OpenSource">
// Copyright (C) Coalition of the Good-Hearted Engineers
// </copyright>
//----------------------------------------------------------------
/*
* ==============================================================
* Copyright (c) Coalition of the Good-Hearted Engineers
* FREE TO USE TO CONNECT THE WORLD
* ==============================================================
*/
Methods that have code that is not accessible at dev-time, or perform a complex function should contain the following details in their documentation.
| Element | Pattern | Example |
|---|---|---|
| Broker interface | I{Resource}Broker | IStorageBroker, IModernApiBroker, ILoggingBroker |
| Broker class | {Resource}Broker | StorageBroker, ModernApiBroker, LoggingBroker |
| Broker method | {Action}{Entity}Async | InsertLegacyUserAsync, PostPersonAsync |
| Service interface | I{Entity}Service | ILegacyUserService |
| Service class | {Entity}Service | LegacyUserService |
| Service method | Add{Entity}Async | AddLegacyUserAsync |
| Inner exception | {Adjective}{Entity}Exception | NullLegacyUserException |
| Outer exception | {Entity}{Category}Exception | LegacyUserValidationException |
| Test class | {Entity}ServiceTests | LegacyUserServiceTests |
| Test method | Should{Action}Async / ShouldThrow{Exception}On{Action}… | ShouldAddLegacyUserAsync |