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.
public record DataStructure(string Required, object? Optional, int WithDefault = 42);
public record DataStructure
{
public required string Required { get; init; }
public object? Optional { get; init; }
public int WithDefault { get; init; } = 42
}