> ## 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.

# C# Optional Parameter

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

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
// Omits 'enableLogging', uses default (true)
ConfigureSystem(101, "Advanced"); 

// Omits 'mode', requires named argument for 'enableLogging'
ConfigureSystem(101, enableLogging: false); 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
