DefaultParameterValue
must be used instead of DefaultValue
when Optional
attribute is specified CS-W1045The 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.
public void Foo([Optional, DefaultValue(4)] int x)
{
}
public void Foo([Optional, DefaultParameterValue(4)] int x)
{
}