开发者

default function parameters in C#

开发者 https://www.devze.com 2023-01-04 11:51 出处:网络
Is there a way to have d开发者_Go百科efault function parameters in C# like we have in C++?? eg: foo(int i = 10, int j= 20) {}

Is there a way to have d开发者_Go百科efault function parameters in C# like we have in C++??

eg:

foo(int i = 10, int j  = 20) {}


Named and optional parameters are new in C# 4.0.


Yes, default parameters are in C# 4.0.


You can do this only in C# 4.0.


if you don't have C# 4 you can define your method twice, like this:

    public int MySillyMethod(int a)
    {
        return MySillyMethod(a, 1);
    }

    public int MySillyMethod(int a, int b)
    {
        return a*b;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消