Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just did a quick test and the compiler optimizes the code. This basic example Main method.</p> <pre><code>public static void OptionalParamMethod(bool input, string input2 = null) { } public static void Main(string[] args) { OptionalParamMethod(true); OptionalParamMethod(false, "hello"); } </code></pre> <p>Compiles to this so the optional params are filled in by the compiler.</p> <pre><code>public static void Main(string[] args) { OptionalParamMethod(true, null); OptionalParamMethod(false, "hello"); } </code></pre> <p>As for performance you could argue optional parameters have a slight advantage as there is only a single method call rather than chained method calls like you would normally have for an overloaded method. The code below is compiled output to show what I am talking about. The <a href="https://gist.github.com/iwantedue/7150746" rel="nofollow">difference</a> is quite academic though and I doubt you would ever notice in practice.</p> <pre><code>public static void Main(string[] args) { OptionalParamMethod(true, null); OptionalParamMethod(false, "hello"); OverloadParamMethod(true); OverloadParamMethod(false, "hello"); } public static void OptionalParamMethod(bool input, [Optional, DefaultParameterValue(null)] string input2) { Console.WriteLine("OptionalParamMethod"); } public static void OverloadParamMethod(bool input) { OverloadParamMethod(input, null); } public static void OverloadParamMethod(bool input, string input2) { Console.WriteLine("OverloadParamMethod"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload