C# / .NET/01intro10 min

C# naming conventions and style

Consistent C# style makes code predictable. ProfessNet sticks to Microsoft's official conventions and enforces them automatically through .editorconfig and Roslyn analyzers — not in PR comments.

Naming

ElementConventionExample
Class, method, propertyPascalCaseProbeService, RunScan
InterfaceI + PascalCaseIProbeRepository
Parameter, local variablecamelCaseforestName
Private field_ + camelCase_repository
ConstantPascalCaseMaxRetries
Filetype nameProbeService.cs
public sealed class ProbeService : IProbeService
{
    private readonly IProbeRepository _repository;
    private const int MaxRetries = 3;

    public async Task<ScanResult> RunScanAsync(string forestName)
    {
        // ...
    }
}

ProfessNet standard: private fields use the _ prefix, asynchronous methods the Async suffix. One public class per file, file name = type name.

var — when the type is obvious

// good — the type is visible on the right-hand side
var probes = new List<Probe>();
var result = await _service.RunScanAsync(forest);

// explicit type — when it is not clear from the right-hand side
int count = GetCount();

ProfessNet standard: use var when the type is clear from the assignment. When it is not obvious (e.g. int vs long), write the type explicitly.

Modern syntax

C# evolves quickly — we use the new constructs that make code shorter.

// expression-bodied members
public string FullHost => $"{Host}.{Forest}";

// pattern matching
var status = probe switch
{
    { IsActive: true } => "online",
    { LastSeen: null } => "never",
    _ => "offline",
};

// target-typed new
List<Probe> probes = new();

Formatting and braces

We use the Allman style (brace on a new line) and always use braces with if, even one-liners.

// good
if (probe is null)
{
    return NotFound();
}

// bad — no braces, easy to make a mistake when adding a line
if (probe is null) return NotFound();

.editorconfig — a single source of truth

We keep style rules in .editorconfig in the solution directory. Visual Studio and dotnet format respect this file.

[*.cs]
indent_style = space
indent_size = 4
csharp_new_line_before_open_brace = all
dotnet_naming_rule.private_fields_underscore.severity = error
csharp_style_var_when_type_is_apparent = true:suggestion
dotnet format --verify-no-changes     # CI: checks style without changes

Tip: enable <Nullable>enable</Nullable> and <TreatWarningsAsErrors>true in .csproj. Then style and nullability warnings block the build before they reach review.


Microsoft's conventions + .editorconfig + dotnet format in CI produce code that looks the same regardless of the author. We move the energy from review to logic, not to brace placement.