Can I give a default value to parameters or optional parameters in C# functions?

后端 未结 6 700
花落未央
花落未央 2020-12-03 09:46

Can I give default parameters in C#?

In C:

void fun(int i = 1)
{
    printf(\"%d\", i);
}

Can we give parameters a default value? I

6条回答
  •  孤街浪徒
    2020-12-03 09:53

    Yes. See Named and Optional Arguments. Note that the default value needs to be a constant, so this is OK:

    public string Foo(string myParam = "default value") // constant, OK
    {
    }
    

    but this is not:

    public void Bar(string myParam = Foo()) // not a constant, not OK
    {
    }
    

提交回复
热议问题