How can you use optional parameters in C#?

前端 未结 23 2552
逝去的感伤
逝去的感伤 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

    In C#, I would normally use multiple forms of the method:

    void GetFooBar(int a) { int defaultBValue;  GetFooBar(a, defaultBValue); }
    void GetFooBar(int a, int b)
    {
     // whatever here
    }
    

    UPDATE: This mentioned above WAS the way that I did default values with C# 2.0. The projects I'm working on now are using C# 4.0 which now directly supports optional parameters. Here is an example I just used in my own code:

    public EDIDocument ApplyEDIEnvelop(EDIVanInfo sender, 
                                       EDIVanInfo receiver, 
                                       EDIDocumentInfo info,
                                       EDIDocumentType type 
                                         = new EDIDocumentType(EDIDocTypes.X12_814),
                                       bool Production = false)
    {
       // My code is here
    }
    

提交回复
热议问题