<?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>Default parameters in C#</title><link>http://blogs.msdn.com/b/ericgu/archive/2011/02/23/default-parameters-in-c.aspx</link><description>From an internal discussion we're having on the advisability of using default parameters in C#: 
 Currently, the pain and limitation of doing overloads forces you to rethink how a method should work. Consider the following: 
 Process(int a); 
 Process</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Default parameters in C#</title><link>http://blogs.msdn.com/b/ericgu/archive/2011/02/23/default-parameters-in-c.aspx#10144303</link><pubDate>Tue, 22 Mar 2011 13:24:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10144303</guid><dc:creator>Ryan CrawCour</dc:creator><description>&lt;p&gt;You raise some interesting points here; points i have myself considered and pondered. Are default parameters a blessing? Or a curse?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10144303" width="1" height="1"&gt;</description></item><item><title>re: Default parameters in C#</title><link>http://blogs.msdn.com/b/ericgu/archive/2011/02/23/default-parameters-in-c.aspx#10133907</link><pubDate>Fri, 25 Feb 2011 05:58:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10133907</guid><dc:creator>Marc Brooks</dc:creator><description>&lt;p&gt;I swore to my teddy bear to NEVER add an override that takes a boolean. I&amp;#39;ve found over the years that the meaning of true or false is so easily lost, and so easily forgotten which is the &amp;quot;without argument&amp;quot; state that I&amp;#39;ve gone to using an Enum that clearly described the behavior.&lt;/p&gt;
&lt;p&gt;Years later:&lt;/p&gt;
&lt;p&gt;var tweets = TweetRepo.GetLatestTweets(tweepId, DirtyWords.ReplaceNaughty);&lt;/p&gt;
&lt;p&gt;Is much clearer than:&lt;/p&gt;
&lt;p&gt;var tweets = TweetRepo.GetLatestTweets(tweepId, true);&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10133907" width="1" height="1"&gt;</description></item><item><title>re: Default parameters in C#</title><link>http://blogs.msdn.com/b/ericgu/archive/2011/02/23/default-parameters-in-c.aspx#10133450</link><pubDate>Thu, 24 Feb 2011 10:32:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10133450</guid><dc:creator>Shuggy</dc:creator><description>&lt;p&gt;The thing I don&amp;#39;t like is that they are most useful as a replacement for a bunch of overloads _but_ the semantics of the defaults is different. Instead of being baked into the dll that defines them they are backed into the dlls that compile against them.&lt;/p&gt;
&lt;p&gt;If you make heavy use of overloads to maintain binary compatibility then it&amp;#39;s a real pain.&lt;/p&gt;
&lt;p&gt;I really wish there was a simple way to just create overloads that deferred to the others (and could have xdoc links to the master one (along with an indication of what the defaults were)&lt;/p&gt;
&lt;p&gt;some thing like:&lt;/p&gt;
&lt;p&gt;///&amp;lt;docref cref=&amp;quot;Frob(int,string)&amp;quot;&amp;gt;&amp;lt;paramref name=&amp;quot;s&amp;quot;&amp;gt; is string.Empty&amp;lt;/docref&amp;gt;&lt;/p&gt;
&lt;p&gt;public Foo Frob(int x)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return Frob(x, string.Empty);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;///&amp;lt;summary&amp;gt;blah blah blah&amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;public Foo Frob(int x, string s)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;note how the paramref is refering to a parameter on the master, not the current method&lt;/p&gt;
&lt;p&gt;for overloads using compile time literals you could do very terse stuff like:&lt;/p&gt;
&lt;p&gt;public overload Foo Frob(int x) ~&amp;gt; Frob(x, &amp;quot;&amp;quot;);&lt;/p&gt;
&lt;p&gt;And have the document system auto fill everything for you. Though often being limited to compile time literals would be a pain I image it would in fact cover 70% of my needs (so long as default(T) was allowed on generic methods).&lt;/p&gt;
&lt;p&gt;And hey, I found a use for the nadj (&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/b/ericlippert/archive/2008/07/14/a-face-made-for-email-part-four.aspx"&gt;blogs.msdn.com/.../a-face-made-for-email-part-four.aspx&lt;/a&gt;) :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10133450" width="1" height="1"&gt;</description></item><item><title>re: Default parameters in C#</title><link>http://blogs.msdn.com/b/ericgu/archive/2011/02/23/default-parameters-in-c.aspx#10133366</link><pubDate>Thu, 24 Feb 2011 05:26:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10133366</guid><dc:creator>Haacked</dc:creator><description>&lt;p&gt;There&amp;#39;s definitely some versioning issues to consider when using optional parameters with defaults.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://haacked.com/archive/2010/08/10/versioning-issues-with-optional-arguments.aspx"&gt;haacked.com/.../versioning-issues-with-optional-arguments.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://haacked.com/archive/2010/08/12/more-optional-versioning-fun.aspx"&gt;haacked.com/.../more-optional-versioning-fun.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This led us to remove much of our usage of them in ASP.NET except for maybe a few places where we didn&amp;#39;t think we&amp;#39;d ever change them.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10133366" width="1" height="1"&gt;</description></item></channel></rss>