Hi,

how to declare optional parameters in C#. Like...
public void test(int a, Optional int b)
Thanks in advance.

Recommended Answers

All 4 Replies

LizR is right. More simply put you can define a single method like this:

public void test(int a){...}
public void test(int a, int b){...}

At run time you can call test with whichever set of parameters you want.

I disagree on LizR. we can an optional parameter.

try this...

private void Form1_Load(object sender, EventArgs e)
{
       sample("Hello World.");
}

private void sample(string str, params object[] mode)
{
            MessageBox.Show(str);
}

hope this would help.

regards.

Not in the way the OP implied eg the equivelent of void myfunc(int param1=0, string param2="default") like a lot of languages support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.