<p>Is it true that the only way to handle default function arguments is through function overloading?</p>

<p>For example, in PHP I can do this:</p>

<pre><code>function foo($x, $y=0)
{
}
</code></pre>

<p>Would the best way to handle it in C# be this?</p>

<pre><code>void foo(int x)
{
  foo(x, 0);
}

void foo(int x, int y)
{
}
</code></pre>

<p><strong><em><a href="http://bytes.com/forum/thread224970.html" rel="noreferrer">Example lifted from here</a></em></strong></p>

<p>Edit</p>

<p><strong><em>Made the C# example into actual C# (Thanks Blair Conrad)</em></strong></p>

[{'answer_id': 40093, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 4, 'selected': True, 'text': "<p>Yes, that'd be best<s>, except you'd omit the <code>$</code>s on the parameter names</s>, as others have pointed out. For those interested in the rationale behind the lack of default parameter values, see @Giovanni Galbo's explanation.</p>\n"}, {'answer_id': 40095, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>Default arguments are part of C++, but as of C# 3.5 default arguments are still not supported-- you'll have to overload.  They've been available in VB.Net since 1.0.</p>\n"}, {'answer_id': 40096, 'author': 'Unsliced', 'author_id': 2902, 'author_profile': 'https://Stackoverflow.com/users/2902', 'pm_score': 0, 'selected': False, 'text': '<p>Yes. </p>\n\n<p>Or currying. </p>\n\n<p>Or abstracting into a class and using default values there. </p>\n'}, {'answer_id': 40098, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 0, 'selected': False, 'text': '<p>No, AFAIK C# does not support overriding, and yes, that is the recommended way of accomplishing the same effect.</p>\n'}, {'answer_id': 40121, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 4, 'selected': False, 'text': '<p>Just to satisfy some curiosity:</p>\n<p>From <a href="http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx" rel="noreferrer">Why doesn\'t C# support default parameters?</a>:</p>\n<blockquote>\n<p>In languages such as C++, a default value can be included as part of the method declaration:</p>\n<p>void Process(Employee employee, bool bonus = false)</p>\n<p>This method can be called either with:</p>\n<p>a.Process(employee, true);</p>\n<p>or</p>\n<p>a.Process(employee);</p>\n<p>in the second case, the parameter bonus is set to false.</p>\n<p>C# doesn\'t have this feature.</p>\n<p>One reason we don\'t have this feature is related to a specific implementation of the feature. In the C++ world, when the user writes:</p>\n<p>a.Process(employee);</p>\n<p>the compiler generates</p>\n<p>a.process(employee, false);</p>\n<p>In other words, the compiler takes the default value that is specified in the method prototype and puts it into the method call - it\'s just as if the user wrote \'false\' as the second parameter. There\'s no way to change that default value without forcing the user of the class to recompile, which is unfortunate.</p>\n<p>The overloading model works better in this respect. The framework author just defines two separate methods, and the single-parameter one calls the two-parameter method. This keeps the default value in the framework, where it can be modified if necessary.</p>\n<p>It would be possible for a compiler to take something like the C++ definition and produce the overloads, but there are a few issues with that approach.</p>\n<p>The first one is that the correlation between the code that the user writes and the code the compiler generates is less obvious. We generally try to limit magic when possible, as it makes it harder for programmers. The second issue has to do with things like XML doc comments and intellisense. The compiler would have to have special rules for how it generates doc comments for the overloaded methods, and intellisense would need to have smarts to collapse the overloaded methods into a single method.</p>\n<p>Writing overloads yourself is a bit less convenient, but we think it\'s an acceptable solution.</p>\n</blockquote>\n'}, {'answer_id': 40235, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>Regarding the <a href="http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85556.aspx" rel="nofollow noreferrer">excerpt from the c# faq</a>:  </p>\n\n<p>Most of the problems listed there were solved for VB.Net (specifically the intellisense and xml comments issues), meaning they\'re really red herrings-- <em>there is code available to the C# team that will solve the problem.</em>  </p>\n\n<p>Another reason has to do with forcing a user of a class to re-compile, but that\'s a bit of a red herring, too.  If you <em>change</em> a default value in your framework class and the user does <em>not</em> have to recompile, you risk the user <em>not knowing that the default value changed.</em>  Now you have a potential bug in the code that doesn\'t show up until runtime.  In other words, the alternative of overloading the function is at least as bad.  Of course, this also presumes a specific implementation of the feature, but it\'s the implementation suggested in the faq.</p>\n\n<p>Therefore you have to weigh the remaining reason ("try to limit the magic") vs the fact (which they acknowledge) that writing the overloads is "a bit less convenient".  Personally, I say put the feature in, and let the programmer decide whether or not to use it.</p>\n'}, {'answer_id': 696656, 'author': 'gazhay', 'author_id': 84545, 'author_profile': 'https://Stackoverflow.com/users/84545', 'pm_score': 0, 'selected': False, 'text': '<p>doe this not do the job?</p>\n\n<pre><code>void foo(int x):this(x, 0){}\n\nvoid foo(int x, int y){\n // code here\n}\n</code></pre>\n'}, {'answer_id': 696676, 'author': 'Ian', 'author_id': 21061, 'author_profile': 'https://Stackoverflow.com/users/21061', 'pm_score': 0, 'selected': False, 'text': '<p>As pointed out, this isn\'t currently available in C# however they will be present within C# 4.0 as Sam Ng discusses on his blog:</p>\n\n<p><a href="http://blogs.msdn.com/samng/archive/2009/02/03/named-arguments-optional-arguments-and-default-values.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/samng/archive/2009/02/03/named-arguments-optional-arguments-and-default-values.aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/40090', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/305/']