Purpose: Comprehensive, non-project-specific guide for AI-assisted .NET development.
- Test-Driven Development: Interface β Test β Implementation
- LINE BREAKS AFTER CLOSING BRACES - AI models frequently break this rule!
- Partial classes for files >250 lines using
ClassName._.cs
naming - Operators at beginning of lines for readability
- Testable code: Clean input/output, interface-driven design
- Modular logic: Small, composable methods and classes
- Immutable & stateless: Favor functional, declarative flows
- Dependency injection: Use interfaces for all swappable behaviors
- Interface First: Define what (not how), ensure mockable
- Test Second: Realistic inputs, edge cases, validate behavior
- Implementation Last: Focused methods, clarity over cleverness
record
andreadonly record struct
for data- Collection expressions,
field
keyword Span<T>
,Memory<T>
,ReadOnlySpan<T>
,ReadOnlyMemory<T>
ReadOnlySpan<char>
over strings,StringSegment
for allocations- Primary constructors when appropriate
AI models frequently edit code and accidentally remove line breaks. ALWAYS ensure proper spacing:
public void MyMethod()
{
// logic here
} // <- CRITICAL: Line break required here!
public void NextMethod() // <- This line must have space above
{
// more logic
}
public class MyClass
{
// class content
} // <- CRITICAL: Line break required here!
public class NextClass // <- This line must have space above
{
// class content
}
Common AI Mistakes to Avoid:
// β WRONG - No line break after brace
public void Method1() { }
public void Method2() { }
// β
CORRECT - Line break after brace
public void Method1() { }
public void Method2() { }
Operator Placement:
return condition
&& FirstCheck()
|| SecondCheck();
- Names: Expressive, no abbreviations, PascalCase for types/files
- Documentation: XML docs (
///
) for public APIs - Async: Never
async void
except event handlers - State: Avoid static state unless required
Critical for AI Development: Large files are harder for AI models to understand and edit safely. AI often corrupts large files when making changes.
- Files >250 lines - Split immediately when approaching this limit
- Multiple concerns - Different logical responsibilities in same class
- AI regeneration safety - Smaller files = less corruption risk
- Better comprehension - AI can focus on specific functionality
- Regeneration Safety: AI can recreate small files without breaking existing code
- Focused Changes: AI makes targeted edits instead of large, risky modifications
- Reduced Errors: Smaller context means fewer mistakes
- Easier Reviews: Humans can validate AI changes more easily
- Primary file:
ClassName._.cs
- Contains: Constructor, finalizer, disposal, XML documentation, core structure
- Underscore makes it sort first in file explorers
- Feature files:
ClassName.FeatureName.cs
- Examples:
Calculator.Operations.cs
,Parser.Validation.cs
,Service.Cache.cs
- Examples:
- Split by logical concern, not arbitrary line counts
- One major feature per file when possible
- Prefer single-method-per-file over large monolithic classes
- Use in test projects with folders for better organization
- When in doubt: Split it - easier to merge than to untangle corruption
- Coverage: All logic unit tested, deterministic & reproducible
- Tools: Prefer
NSubstitute
and avoidMoq
, useVerify
for snapshots - Organization: Folders + partial classes in test projects
- Approach: Test-first encouraged, realistic inputs + edge cases
- Use
Verify
for XML, SVG, JSON validation - Snapshot tests for rendered output encouraged
- Reuse existing interfaces (don't redefine)
- Write tests alongside code
- Ask for confirmation when uncertain
- Add
TODO
comments for stubs/placeholders - Read change-logs before making changes
- Auto-generate boilerplate/scaffolding
- Modify
.editorconfig
,.github/
, directive files - Make speculative refactors
- Logical namespaces/folders, meaningful names
- Separate test projects, related functionality together
- Dependency injection for swappable behaviors
- Interfaces for external dependencies
- Follow
.editorconfig
rules, modern project formats - Up-to-date packages, nullable reference types
- Clean builds (no warnings), appropriate target frameworks
This guide optimizes AI-assisted .NET/C# development for quality, consistency, and maintainability. All generated code should reflect experienced developer standards in modern, test-driven, interface-oriented environments.