<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx</link><description>(This is part one of a series on the corner cases of optional arguments in C# 4. Part two is here .) In C# 4.0 we added "optional arguments"; that is, you can state in the declaration of a method's parameter that if certain arguments are omitted, then</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162837</link><pubDate>Tue, 10 May 2011 08:04:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162837</guid><dc:creator>Graham Clark</dc:creator><description>&lt;p&gt;I&amp;#39;m a bit confused by the warning the compiler gives you if both the interface and an explicit implementation give a default value for a parameter: &lt;/p&gt;
&lt;p&gt;&amp;quot;The default value specified for parameter &amp;#39;x&amp;#39; will have no effect because it applies to a member that is used in contexts that do not allow optional arguments&amp;quot;&lt;/p&gt;
&lt;p&gt;Surely optional arguments *are* allowed in this case, it&amp;#39;s just that the one provided on the explicit implementation won&amp;#39;t be used?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162837" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162835</link><pubDate>Tue, 10 May 2011 07:57:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162835</guid><dc:creator>Graham Clark</dc:creator><description>&lt;p&gt;The compiler would also give you an error about using the &amp;quot;public&amp;quot; modifier in your interface!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162835" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162740</link><pubDate>Tue, 10 May 2011 00:10:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162740</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;@Brett:&lt;/p&gt;
&lt;p&gt;VB requires that classes repeat exactly any optional/defaulted arguments from the interfaces they implement - it&amp;#39;s considered part of the signature of the method. If you omit the default value on your implementation, the compiler will complain that &amp;quot;there is no matching sub/function on the interface&amp;quot;&lt;/p&gt;
&lt;p&gt;The obvious question, then, is why C# couldn&amp;#39;t do the same - i.e. refuse to consider method an implementation if values of optional arguments don&amp;#39;t match the interface, and then ultimately complain that class didn&amp;#39;t properly implement an interface it listed because no matching implementing method was found.&lt;/p&gt;
&lt;p&gt;My tentative guess is that this would have broken back-compat for cases where existing C# code implements interfaces defined in VB with optional arguments - since any pre-4.0 C# code wouldn&amp;#39;t have used default values in such cases, and hence be broken by 4.0.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162740" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162712</link><pubDate>Mon, 09 May 2011 22:20:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162712</guid><dc:creator>Brett</dc:creator><description>&lt;p&gt;How did the VB compiler handle these cases?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162712" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162707</link><pubDate>Mon, 09 May 2011 21:56:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162707</guid><dc:creator>Brian</dc:creator><description>&lt;p&gt;@InBetween: True, but in the case of unreachable code there is a practical situation where a programmer might temporarily want the code unreachable but would still prefer a warning. &amp;nbsp;In particular, this applies in the case where the coder is stuffing a return in the middle of a function for quick and dirty debugging purposes. &amp;nbsp;In contrast to unreachable code, I cannot think of a case where a programmer would want to leave such a useless piece of code in place. &amp;nbsp;It might also apply in the case that a programmer is using Macros...though usually the macros would be used to kill the unreachable code, too.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162707" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162683</link><pubDate>Mon, 09 May 2011 20:58:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162683</guid><dc:creator>John Bailo</dc:creator><description>&lt;p&gt;Initially I would have wanted this...but seeing it in this use case, I&amp;#39;m sorry but I wish they had stuck to their guns and not tried to make c# more &amp;quot;like VB&amp;quot;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162683" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162625</link><pubDate>Mon, 09 May 2011 18:47:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162625</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;@ErikF: allowing optional arguments on interfaces lets users of those interfaces omit them when calling methods via interface-typed references. &lt;/p&gt;
&lt;p&gt;Not allowing optional arguments on interfaces would also defeat a very major use case for them, which is COM interop &amp;nbsp;(because methods with 10+ arguments, only 1 or 2 of which are required, is fairly common there - if you ever saw Office APIs, you know what I mean).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162625" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162538</link><pubDate>Mon, 09 May 2011 15:58:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162538</guid><dc:creator>InBetween</dc:creator><description>&lt;p&gt;@Brian&lt;/p&gt;
&lt;p&gt;I would gues its the same as having unreachable code.&lt;/p&gt;
&lt;p&gt;There is no error per se. The explicit implementation of the interface means that the methods the interface defines can only be called through the interface itself and therefore the optional parameters defined in the class impementing the interface are &amp;quot;unreachable&amp;quot;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162538" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162522</link><pubDate>Mon, 09 May 2011 15:26:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162522</guid><dc:creator>Brian</dc:creator><description>&lt;p&gt;&amp;quot;This also explains why the compiler gives you a stern warning when you do an explicit interface implementation&amp;quot;&lt;/p&gt;
&lt;p&gt;You probably get asked this way too often, but What is the justification for allowing that code to compile with a warning rather than with an error?&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162522" width="1" height="1"&gt;</description></item><item><title>re: Optional argument corner cases, part one</title><link>http://blogs.msdn.com/b/ericlippert/archive/2011/05/09/optional-argument-corner-cases-part-one.aspx#10162514</link><pubDate>Mon, 09 May 2011 15:21:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162514</guid><dc:creator>ErikF</dc:creator><description>&lt;p&gt;Seeing that interfaces can&amp;#39;t have any code, I&amp;#39;m trying to understand how allowing optional arguments for an interface method would be helpful. &amp;nbsp;Couldn&amp;#39;t you simply forbid optional arguments in interfaces altogether?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162514" width="1" height="1"&gt;</description></item></channel></rss>