topgun_ivard
topgun_ivard

Reputation: 8564

default function parameters in C#

Is there a way to have default function parameters in C# like we have in C++??

eg:

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

Upvotes: 1

Views: 364

Answers (4)

Michiel
Michiel

Reputation: 4260

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;
    }

Upvotes: 0

scatterbraiin
scatterbraiin

Reputation: 177

You can do this only in C# 4.0.

Upvotes: 0

Daniel Brückner
Daniel Brückner

Reputation: 59655

Named and optional parameters are new in C# 4.0.

Upvotes: 5

Tim Jarvis
Tim Jarvis

Reputation: 18815

Yes, default parameters are in C# 4.0.

Upvotes: 3

Related Questions