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.

An optional parameter in C# is a method parameter that specifies a default value in its declaration, allowing the caller to omit the corresponding argument during invocation. When an argument is omitted, the compiler automatically inserts the default value directly into the Intermediate Language (IL) at the call site.

Syntax

public void ConfigureSystem(int requiredId, string mode = "Standard", bool enableLogging = true)
{
    // Implementation
}

Rules and Constraints

  • Positioning: All optional parameters must appear after all required parameters in the method signature. The only exception is a params array, which must appear after all optional parameters.
  • Compile-Time Constants: The default value must be a compile-time constant. Valid values include literals (e.g., 42, "text"), const variables, default(T), or parameterless value type instantiations (e.g., new TimeSpan()). You cannot use variables or method calls.
  • Reference Types: Default values for reference types must generally be null. However, there are exceptions: the string type accepts string literals, and types such as object, dynamic, or specific interfaces (e.g., IEnumerable<char>, IComparable) can accept non-null compile-time constants (like numeric or string literals) due to the compiler allowing implicit boxing and reference conversions. For example, void Configure(object obj = 42) and void Parse(IEnumerable<char> text = "default") are valid. You cannot use reference type instantiations (e.g., new List<int>()) as default values.
  • Parameter Modifiers: Optional parameters cannot be decorated with ref or out modifiers.

Polymorphism and Static Type Resolution

Because default values are resolved and injected at compile-time, the default value applied depends entirely on the static type of the reference at the call site, not the runtime type of the object. If an interface and an implementing class define different default values for the same optional parameter, the compiler will emit the value associated with the variable’s declared type.
public interface IProcessor
{
    void Process(int count = 10);
}

public class DataProcessor : IProcessor
{
    public void Process(int count = 5) { }
}

// Invocation
DataProcessor concrete = new DataProcessor();
concrete.Process(); // Compiler injects 5

IProcessor interfaceRef = concrete;
interfaceRef.Process(); // Compiler injects 10

Compiler Implementation and Versioning

Under the hood, the C# compiler implements optional parameters by applying the [Optional] and [DefaultParameterValue] attributes from the System.Runtime.InteropServices namespace to the parameter in the compiled assembly. Because the compiler bakes the default value into the calling assembly’s IL, optional parameters introduce a strict versioning consideration. If a class library updates the default value of an optional parameter, any dependent assemblies will continue to pass the old default value until they are explicitly recompiled against the new version of the library.

Invocation Mechanics

When a method has multiple optional parameters, arguments can be omitted positionally from right to left. To omit an argument for an optional parameter while providing an argument for a subsequent optional parameter, the caller must use named arguments to explicitly map the value to the correct parameter.
// Omits 'enableLogging', uses default (true)
ConfigureSystem(101, "Advanced"); 

// Omits 'mode', requires named argument for 'enableLogging'
ConfigureSystem(101, enableLogging: false); 
Master C# with Deep Grasping Methodology!Learn More