Back
Close

Optional parameters in C#

gpeipman
11.8K views

Optional parameters

C# supports optional parameters when working with methods. Optional are parameters that have default value specified like shown in the following example. It is convenient way to write methods that have default values specified for their arguments.

using System;
class Hello
{
static void Main()
{
Console.WriteLine(SomeMethod());
Console.WriteLine(SomeMethod(null) ?? "<null>");
Console.WriteLine(SomeMethod("specified value"));
}
static string SomeMethod(string s1 = "default value")
{
return s1;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Optional parameters must be the last ones in method arguments list.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants