Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A named parameter in C# is a language feature that allows a caller to pass an argument to a method, constructor, delegate, or indexer by explicitly associating the argument value with the parameter’s declared name, bypassing the standard positional matching mechanism. Technically referred to as a named argument at the call site, this feature shifts the binding of arguments to parameters from index-based resolution to name-based resolution during compilation.
MethodName(parameterName: argumentValue);

Syntax and Mechanics

When invoking a method, you prefix the argument expression with the parameter’s exact identifier followed by a colon (:).
public void InitializeNode(string address, int port, bool useSsl) { }

// Fully named invocation
InitializeNode(port: 443, useSsl: true, address: "10.0.0.1");
Because the compiler resolves the bindings by name, the order in which the arguments are provided in the calling expression does not need to match the order defined in the method signature.

Polymorphism and Static Typing

Named arguments are resolved at compile-time against the static type of the reference used for the invocation, not the runtime type of the object. If an interface or base class defines a parameter with one name, but the implementing or derived class uses a different name, the caller must use the parameter name defined by the static type of the variable they are using.
public interface IProcessor
{
    void Process(int inputData);
}

public class DataProcessor : IProcessor
{
    // Parameter name differs from the interface declaration
    public void Process(int rawValue) { }
}

IProcessor interfaceRef = new DataProcessor();
DataProcessor concreteRef = new DataProcessor();

// Valid: Resolved against the static type IProcessor
interfaceRef.Process(inputData: 42); 

// Valid: Resolved against the static type DataProcessor
concreteRef.Process(rawValue: 42); 

// Invalid: Compiler Error CS1739. 'rawValue' does not exist on IProcessor
// interfaceRef.Process(rawValue: 42); 

Interaction with Optional Parameters

Named arguments are fundamentally integrated with optional parameters. When a method signature defines multiple optional parameters, named arguments allow the caller to provide values for specific optional parameters while omitting others, without needing to supply arguments for every positional parameter in between.
public void ConfigureSystem(int timeout = 30, bool enableLogging = false, string mode = "default") { }

// Skips 'enableLogging' by explicitly naming 'mode'
ConfigureSystem(timeout: 60, mode: "verbose");

Mixing Positional and Named Arguments

C# allows the mixing of standard positional arguments and named arguments within the same invocation, subject to strict compiler rules regarding ordering.
  1. Positional First: Positional arguments can always precede named arguments. Once a named argument is used out of its declared order, all subsequent arguments must also be named.
  2. C# 7.2+ Positional Trailing: Starting in C# 7.2, a named argument can be followed by a positional argument only if the named argument is placed in its correct ordinal position according to the method signature.
// Valid: Positional precedes named
InitializeNode("10.0.0.1", useSsl: true, port: 443);

// Valid (C# 7.2+): Named argument is in the correct ordinal position (0)
InitializeNode(address: "10.0.0.1", 443, true);

// Invalid: Positional argument follows an out-of-order named argument
// InitializeNode(port: 443, "10.0.0.1", true); // Compiler Error CS1738

Evaluation Order

When using named arguments out of order, the evaluation of the argument expressions occurs in the order they appear in the calling expression, not the order they are declared in the method signature.
// GetPort() is executed first, followed by GetAddress(), then GetSslFlag()
InitializeNode(port: GetPort(), address: GetAddress(), useSsl: GetSslFlag());

Overload Resolution and Modifiers

C# does not allow method overloading based solely on parameter names. A method’s signature for overloading purposes consists strictly of its name and the number, types, and modifiers of its parameters. However, during overload resolution, the compiler evaluates named arguments to filter applicable methods. If a named argument does not match any parameter name in the available valid overloads, the compiler emits error CS1739. Furthermore, named arguments cannot be used to bypass ref, out, or in modifiers; the modifier must still precede the argument value at the call site.
public void UpdateState(ref int stateCode) { }

// Correct syntax for named argument with a reference modifier
UpdateState(stateCode: ref currentCode);
Master C# with Deep Grasping Methodology!Learn More