Setting the default value of a C# Optional Parameter

后端 未结 3 1904
甜味超标
甜味超标 2020-12-01 17:58

Whenever I attempt to set the default value of an optional parameter to something in a resource file, I get a compile-time error of

Default parameter

3条回答
  •  悲&欢浪女
    2020-12-01 18:29

    Another option is to split your method into two, and have the one overload call the other, like so:

    public void ValidationError(string fieldName)
    { 
        ValidationError(fieldName, ValidationMessages.ContactNotFound);
    }
    
    public void ValidationError(string fieldName, string message)
    {
        ...
    }
    

    This way also enables you to pass null as a value for message in case that is also a valid value for that parameter.

提交回复
热议问题