C#

C#

Made by DeepSource

DefaultParameterValue must be used instead of DefaultValue when Optional attribute is specified CS-W1045

Bug risk
Major
Autofix

The Optional attribute signifies that the parameter is optional, meaning the caller does not have to pass its value explicitly. In such cases, it is ideal to specify a default value. However, it is easy to get confused with DefaultValue and DefaultParameterValue attributes as the former is used for fields rather than parameters. Therefore, consider using the DefaultParameterValue attribute instead.

Bad Practice

public void Foo([Optional, DefaultValue(4)] int x)
{
}

Recommended

public void Foo([Optional, DefaultParameterValue(4)] int x)
{
}

Reference