Back
Close

Named parameters in C#

gpeipman
9,193 views

Named parameters

Related to optional parameters in C# are named parameters at method calls. There are libraries with classes that have long argument lists and when calling these methods we usually have to give only the parameters we know.

The following example shows how to call method with named parameters to save time and win on readability of code.

using System;
class Hello
{
static void Main()
{
SomeMethod(value2: "Second value");
}
static void SomeMethod(string value1 = "value1", string value2 = "value2", string value3 = "value3" /* etc */)
{
Console.WriteLine("value1: " + value1);
Console.WriteLine("value2: " + value2);
Console.WriteLine("value3: " + value3);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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