Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 1.08 KB

QW0016.md

File metadata and controls

35 lines (27 loc) · 1.08 KB

QW0016: Prefer regular over positional properties

With the release of C# 10, record's have been introduced. One of the advocated advantages is the possibility to use a so called primary constructor which allows you to define a full data structure in a single line:

record MyRecord(string Message, string Other);

However powerful, for (public) APIs it turns out to be not that practical: Defining attributes or adding documentation turns out to be counterintuitive and cumbersome. Having numerous properties (let's say 3 or more) also makes it less readable.

Therefore this rule reports on primary constructors on public records, and provides a code fix to help refactoring.

Non-compliant

public record DataStructure(string Required, object? Optional, int WithDefault = 42);

Compliant

public record DataStructure
{
    public required string Required { get; init; }
    
    public object? Optional { get; init; }
    
    public int WithDefault { get; init; } =  42
}