GU0003
November 24, 2021 ยท View on GitHub
Name the parameter to match the assigned member
| Topic | Value |
|---|---|
| Id | GU0003 |
| Severity | Hidden |
| Enabled | True |
| Category | Gu.Analyzers.Correctness |
| Code | ConstructorAnalyzer |
Description
Name the constructor parameters to match the assigned member. This is useful when serializing using NewtonSoft.Json and when using named arguments.
Motivation
In the below code the parameter named notMatching should be named a to match the property it sets.
public class Foo
{
public Foo(int notMatching, int b, int c, int d)
{
this.A = notMatching;
this.B = b;
this.C = c;
this.D = d;
}
public int A { get; }
public int B { get; }
public int C { get; }
public int D { get; }
}
How to fix violations
public class Foo
{
public Foo(int a, int b, int c, int d)
{
this.A = a;
this.B = b;
this.C = c;
this.D = d;
}
public int A { get; }
public int B { get; }
public int C { get; }
public int D { get; }
}
Configure severity
Via ruleset file.
Configure the severity per project, for more info see MSDN.
Via #pragma directive.
#pragma warning disable GU0003 // Name the parameter to match the assigned member
Code violating the rule here
#pragma warning restore GU0003 // Name the parameter to match the assigned member
Or put this at the top of the file to disable all instances.
#pragma warning disable GU0003 // Name the parameter to match the assigned member
Via attribute [SuppressMessage].
[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness",
"GU0003:Name the parameter to match the assigned member",
Justification = "Reason...")]