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.

Positional parameters refer to the default argument-passing mechanism in C# where an argument is mapped to a method, constructor, indexer, or attribute parameter strictly based on its ordinal position in the invocation list. The compiler resolves the binding by matching the first argument in the caller to the first parameter in the signature, the second to the second, and so forth.

Syntax and Binding Mechanics

In a standard invocation, the sequence of provided arguments must exactly match the sequence and implicitly convertible types of the parameters defined in the member signature.
public void ConfigureNode(string address, int port, bool isActive)
{
    // Parameter definitions dictate the required positional order
}

// Positional invocation: 
// Index 0 maps to 'address', Index 1 maps to 'port', Index 2 maps to 'isActive'
ConfigureNode("127.0.0.1", 8080, true);

Rules for Mixing with Named Arguments

C# allows mixing positional arguments with named arguments, subject to strict compiler rules regarding evaluation order and positioning:
  1. Positional First: Positional arguments must generally precede named arguments.
  2. C# 7.2+ Relaxation: A named argument can precede a positional argument only if the named argument is placed in its correct ordinal position.
// Valid: All positional
ConfigureNode("127.0.0.1", 8080, true);

// Valid: Positional preceding named
ConfigureNode("127.0.0.1", 8080, isActive: true);

// Valid (C# 7.2+): Named in correct position preceding positional
ConfigureNode(address: "127.0.0.1", 8080, true);

// Compiler Error: Named argument out of position preceding positional
// ConfigureNode(port: 8080, "127.0.0.1", true); 

Positional Parameters in Attributes

In the context of C# Attributes, the language enforces a strict syntactic distinction between positional parameters and named parameters. Positional parameters map directly to the attribute’s constructor signature, whereas named parameters map to public, non-static, read-write fields or properties.
public class EndpointAttribute : Attribute
{
    // Constructor defines positional parameters
    public EndpointAttribute(string route) { }

    // Property defines a named parameter
    public int Timeout { get; set; }
}

// "api/v1/data" is passed positionally to the constructor.
// Timeout is passed as a named parameter.
[Endpoint("api/v1/data", Timeout = 5000)]
public class DataController { }

Positional Syntax in Records (C# 9.0+)

C# 9.0 introduced “positional syntax” for record types. When a record is declared with a parameter list directly following the type name, the compiler automatically generates a primary constructor, public init-only properties, and a Deconstruct method whose ordinal positions match the declaration.
// Positional record declaration
public record Vector3(float X, float Y, float Z);

// Positional instantiation via the generated primary constructor
var vector = new Vector3(1.0f, 0.0f, -1.5f);

// Positional deconstruction
var (x, y, z) = vector; 
Master C# with Deep Grasping Methodology!Learn More