How can you use optional parameters in C#?

前端 未结 23 2540
逝去的感伤
逝去的感伤 2020-11-22 17:02

Note: This question was asked at a time when C# did not yet support optional parameters (i.e. before C# 4).

We\'re building a web API tha

23条回答
  •  遥遥无期
    2020-11-22 17:45

    I agree with stephenbayer. But since it is a webservice, it is easier for end-user to use just one form of the webmethod, than using multiple versions of the same method. I think in this situation Nullable Types are perfect for optional parameters.

    public void Foo(int a, int b, int? c)
    {
      if(c.HasValue)
      {
        // do something with a,b and c
      }
      else
      {
        // do something with a and b only
      }  
    }
    

提交回复
热议问题