C# supply values for all optional parameters

后端 未结 3 1343
南方客
南方客 2021-01-19 21:25

I\'m reading a book for C# and I\'m in the chapter of named and optional parameters. I\'ve read a bullet where it says:

\"If multiple optional paramet

3条回答
  •  悲哀的现实
    2021-01-19 22:02

    It simply means that optional parameters must all be last in the parameter list.

    public void Method(int param1, int param2 = 0, int param3 = 0)
    {
        //This works
    }
    

    versus

    public void Method(int param1 = 0, int param2 = 0, int param3) //Param3 isn't optional.
    {
        //This does not!
    }
    
    public void Method(int param1 = 0, int param2, int param3 = 0) //Param2 isn't optional.
    {
        //Neither does this!
    }
    

提交回复
热议问题