Check inside method whether some optional argument was passed

前端 未结 10 1578
眼角桃花
眼角桃花 2020-11-30 14:00

How do I check if an optional argument was passed to a method?

public void ExampleMethod(int required, string optionalstr = \"default string\",
    int optio         


        
10条回答
  •  鱼传尺愫
    2020-11-30 14:37

    To determine whether an argument has been passed to an optional parameter

    1. Define an extremely unlikely value as the default for the parameter.
    2. If the optional parameter is a reference type such as a String, you can use null as the default value, provided this is not an expected value for the argument.
    3. In the procedure code, compare the parameter against the default value and take the appropriate action.

    https://msdn.microsoft.com/en-us/library/849zff9h(v=vs.100).aspx

提交回复
热议问题