|
1 | | ---- |
2 | | -applyTo: '**/*.cs' |
3 | | -description: 'C# best practices and formatting guidelines for AI code generation (all versions).' |
4 | | ---- |
5 | | - |
6 | | -# C# Best Practices for AI Code Generation Instructions |
7 | | - |
8 | | -## Code Style and Formatting |
9 | | - |
10 | | -### Namespace Declarations |
11 | | - |
12 | | -- Use file-scoped namespace declarations when using C# 10+ (`namespace MyNamespace;`) |
13 | | -- For older versions, use traditional block-scoped namespaces with proper indentation |
14 | | -- Keep namespace declarations consistent within the project |
15 | | - |
16 | | -### Access Modifiers |
17 | | - |
18 | | -- Explicitly declare access modifiers for all members |
19 | | -- Use `readonly` for fields that are only assigned in constructors |
20 | | -- Use `const` for compile-time constants |
21 | | -- Follow principle of least privilege for access levels |
22 | | - |
23 | | -### Type Declarations |
24 | | - |
25 | | -- Use `var` for local variables when the type is obvious from the right side |
26 | | -- Use explicit types for method parameters, return types, and field declarations |
27 | | -- Prefer target-typed expressions when the type is clear from context |
28 | | -- Use meaningful and descriptive type names |
29 | | - |
30 | | -### Method and Property Formatting |
31 | | - |
32 | | -- Use expression-bodied members for simple one-line implementations |
33 | | -- Place opening braces on new lines for methods, classes, and namespaces |
34 | | -- Use auto-implemented properties when appropriate |
35 | | -- Implement properties with backing fields only when additional logic is needed |
36 | | - |
37 | | -## Modern C# Patterns |
38 | | - |
39 | | -### Null Safety |
40 | | - |
41 | | -- Use null-conditional operators (`?.`, `??`, `??=`) when available |
42 | | -- Validate parameters with appropriate null checks |
43 | | -- Use `string.IsNullOrEmpty()` and `string.IsNullOrWhiteSpace()` for string validation |
44 | | -- Consider nullable reference types when using C# 8+ |
45 | | - |
46 | | -### String Handling |
47 | | - |
| 1 | +# (File removed) |
48 | 2 | - Use string interpolation `$""` instead of `String.Format()` or concatenation |
49 | 3 | - Use `StringBuilder` for extensive string manipulation |
50 | 4 | - Prefer `string.Equals()` with `StringComparison` for culture-aware comparisons |
|
0 commit comments