Expert guidance for C# 13 development with strict formatting rules, nullable reference types, xUnit testing, and file-scoped namespaces. Enforces .editorconfig standards and modern C# patterns.
Expert assistant for C# 13 development following strict .NET library conventions, modern language features, and comprehensive testing practices.
When working with C# and .NET library projects, follow these guidelines strictly:
1. **High Confidence Reviews**: Make only high confidence suggestions when reviewing code changes. Avoid speculative or low-certainty recommendations.
2. **Latest C# Features**: Always use the latest C# version features (currently C# 13). Leverage pattern matching, switch expressions, and modern syntax.
3. **Protected Files**: Never modify these files unless explicitly requested:
- `global.json`
- `package.json` or `package-lock.json`
- `NuGet.config`
Apply these formatting rules consistently:
1. **EditorConfig Compliance**: Always follow the code-formatting style defined in `.editorconfig`.
2. **Namespace Declarations**: Prefer file-scoped namespace declarations:
```csharp
namespace MyProject.MyFeature;
public class MyClass { }
```
3. **Using Directives**: Use single-line using directives at the top of files.
4. **Code Block Bracing**: Insert a newline before the opening curly brace of any code block:
```csharp
if (condition)
{
// code
}
for (int i = 0; i < count; i++)
{
// code
}
```
5. **Method Returns**: Ensure that the final return statement of a method is on its own line with proper spacing.
6. **Pattern Matching**: Use pattern matching and switch expressions wherever possible instead of traditional if-else chains or switch statements.
7. **nameof Operator**: Use `nameof` instead of string literals when referring to member names:
```csharp
throw new ArgumentNullException(nameof(parameter));
```
8. **XML Documentation**: Create XML doc comments for all public APIs. When applicable, include `<example>` and `<code>` sections:
```csharp
/// <summary>
/// Processes the specified input.
/// </summary>
/// <param name="input">The input to process.</param>
/// <returns>The processed result.</returns>
/// <example>
/// <code>
/// var result = processor.Process("test");
/// </code>
/// </example>
public string Process(string input) { }
```
Follow these strict nullable reference type guidelines:
1. **Non-Nullable by Default**: Declare variables as non-nullable, and perform null checks at entry points only.
2. **Null Comparison Syntax**: Always use `is null` or `is not null` instead of `== null` or `!= null`:
```csharp
if (value is null) { }
if (value is not null) { }
```
3. **Trust Type Annotations**: Trust the C# null annotations and don't add redundant null checks when the type system indicates a value cannot be null.
Follow these testing conventions:
1. **xUnit SDK v3**: Use xUnit SDK v3 for all tests.
2. **No AAA Comments**: Do not emit "Act", "Arrange", or "Assert" comments in test methods. The code should be self-explanatory.
3. **Mocking**: Use Moq for mocking dependencies in tests.
4. **Test Naming**: Copy existing style in nearby test files for method names and capitalization conventions. Maintain consistency within the test suite.
5. **Test Execution**: To build and run tests, use the `build.sh` script located in each subdirectory within the `src` folder:
```bash
./src/Http/build.sh -test
```
```csharp
namespace MyLibrary.Core;
public sealed class DataProcessor
{
public Result Process(Request? request)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
return request.Type switch
{
RequestType.Simple => ProcessSimple(request),
RequestType.Complex => ProcessComplex(request),
_ => throw new NotSupportedException($"Unknown type: {request.Type}")
};
}
}
```
```csharp
namespace MyLibrary.Core.Tests;
public sealed class DataProcessorTests
{
[Fact]
public void Process_WithNullRequest_ThrowsArgumentNullException()
{
var processor = new DataProcessor();
var exception = Assert.Throws<ArgumentNullException>(() => processor.Process(null));
Assert.Equal("request", exception.ParamName);
}
[Theory]
[InlineData(RequestType.Simple)]
[InlineData(RequestType.Complex)]
public void Process_WithValidRequest_ReturnsResult(RequestType type)
{
var processor = new DataProcessor();
var request = new Request { Type = type };
var result = processor.Process(request);
Assert.NotNull(result);
}
}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/net-library-development-standards/raw