Read the syntax
Name selected parameters, supply defaults and understand argument evaluation order.
The second names the boolean so the purpose of true is visible.
What it is and why to use it
A call with several booleans or similar numbers can be hard to read. Named arguments identify the intended parameter directly, while optional parameters provide declared defaults. These features improve an API when used sparingly, but changing a public parameter name or default can affect callers.
How it works
A named argument uses parameterName: expression. Optional defaults are defined on the parameter declaration and must satisfy C# default-value rules. Arguments are evaluated in the order written at the call site, even when named arguments are arranged differently from declaration order. Compiled callers can embed optional defaults, so a library change may require callers to recompile.
Common mistakes and limitations
Do not assume defaults are dynamically read from the callee’s latest assembly. Do not use many optional booleans where a clearer options type is needed. Follow the language’s ordering rules when mixing positional and named arguments; all-named calls can be clearest for reordered values.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
static string Label(string title, bool featured = false) => featured ? "* " + title : title;Examples
Omit a default or override it by name
Use a small formatting helper with one optional setting.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Console.WriteLine(Label("Arrays"));
Console.WriteLine(Label("Functions", featured: true));
static string Label(string title, bool featured = false) => featured ? "* " + title : title;
- The first call omits featured and uses its declared false default.
- The second names the boolean so the purpose of true is visible.
- The helper changes the display prefix only; an optional parameter is not runtime configuration storage.
- Expected output: Arrays * Functions
- Use named arguments to explain otherwise ambiguous values.
Observe call-site evaluation order
Reorder named arguments whose expressions print a trace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
int result = Area(height: Trace("height", 2), width: Trace("width", 3));
Console.WriteLine(result);
static int Trace(string name, int value) { Console.WriteLine(name); return value; }
static int Area(int width, int height) => width * height;
- The height expression appears first in the call and is evaluated first.
- Parameter names still route the two returned values into the correct parameters.
- The final multiplication produces six. Prefer side-effect-free argument expressions in real code when evaluation order could be confusing.
- Expected output: height width 6
- Use named arguments to explain otherwise ambiguous values.
Best practices
- Use named arguments to explain otherwise ambiguous values.
- Treat public parameter names and defaults as part of the caller contract.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference