Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can you use optional parameters in C#?

We're building a web API that's programmatically generated from a C# class. The class has method GetFooBar(int a, int b) and the API has a method GetFooBar taking query params like &a=foo &b=bar.

The classes needs to support optional parameters, which isn't supported in C# the language. What's the best approach?
by

2 Answers

akshay1995

public void SomeMethod(int a, int b = 0)
{
//some code
}
RoliMishra
Another option is to use the params keyword

public void DoSomething(params object[] theObjects)
{
foreach(object o in theObjects)
{
// Something with the Objects…
}
}

Called like...

DoSomething(this, that, theOther);

Login / Signup to Answer the Question.