How can you use optional parameters in C#?

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

    You can use optional parameters in C# 4.0 without any worries. If we have a method like:

    int MyMetod(int param1, int param2, int param3=10, int param4=20){....}
    

    when you call the method, you can skip parameters like this:

    int variab = MyMethod(param3:50; param1:10);
    

    C# 4.0 implements a feature called "named parameters", you can actually pass parameters by their names, and of course you can pass parameters in whatever order you want :)

提交回复
热议问题