Using an enum as an optional parameter

前端 未结 3 689
花落未央
花落未央 2020-12-31 00:27

I have several methods in an application I\'m working on loaded with optional parameters, some of which are enums. Currently, in order to do that I\'m writing methods with a

3条回答
  •  感情败类
    2020-12-31 01:17

    I would suggest using nullable enum in this situation, like this:

    public void SomeMethod(string myFirstParam = "", 
                           string mySecondParam = "", 
                           MyEnum? myThirdParam = null)
    {
       if (myThirdParam.HasValue)
       {
          var enumValue = myThirdParam.Value;
          //do something with it
       }
    }
    

    and you can use it like this:

    SomeMethod(myThirdParam: MyEnum.Something);
    

提交回复
热议问题