<?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>Sam Ng's Blog : Optional arguments</title><link>http://blogs.msdn.com/samng/archive/tags/Optional+arguments/default.aspx</link><description>Tags: Optional arguments</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Named and Optional arguments – ties and philosophies</title><link>http://blogs.msdn.com/samng/archive/2009/04/17/named-and-optional-arguments-ties-and-philosophies.aspx</link><pubDate>Sat, 18 Apr 2009 01:19:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9554683</guid><dc:creator>samng</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/samng/comments/9554683.aspx</comments><wfw:commentRss>http://blogs.msdn.com/samng/commentrss.aspx?PostID=9554683</wfw:commentRss><description>&lt;p&gt;Okay, my attempt at a clever title failed… Ties and Philosophers? I oughtta stick with technical writing. :)&lt;/p&gt;  &lt;p&gt;We’re almost done with our chat about named and optional arguments. We’ve covered &lt;a href="http://blogs.msdn.com/samng/archive/2009/02/03/named-arguments-optional-arguments-and-default-values.aspx"&gt;what the feature is about&lt;/a&gt;, and covered &lt;a href="http://blogs.msdn.com/samng/archive/2009/04/01/named-arguments-and-overload-resolution.aspx"&gt;overload resolution in more detail&lt;/a&gt;. This time I want to do a quick wrap up of our discussion by talking about the tie breaker rules, and then I want to give a bit of background and philosophy behind why we’re electing to do this feature now, instead of several releases ago when it was first considered. &lt;/p&gt;  &lt;h5&gt;Tie breakers&lt;/h5&gt;  &lt;p&gt;During overload resolution, the compiler may find that there are two or more candidates that are perfectly legal candidates given the arguments specified. When that happens, we apply our tie breaker rules to figure out which of the methods is the best one.&lt;/p&gt;  &lt;p&gt;In the past, breaking ties was simple (well, not really, but simpler than it will be now!). Because every candidate had the same number of arguments, (again, not really – param arrays! I’ll explain that in a sec) we simply needed to apply our betterness rules to the conversions to see if we could find a best method. This check amounted to taking each pair of candidates, and checking if either of them has equal-or-better conversions for each parameter. If so, that method was considered the better choice, and was chosen as the best method to bind to. Optional arguments throw a wrench in the mix – now we can have two candidates that are both perfectly valid, but have a different number of parameters. &lt;/p&gt;  &lt;p&gt;First, a quick example:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;object &lt;/span&gt;o) { }
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;x) { }

    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;C &lt;/span&gt;c = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C&lt;/span&gt;();
        c.Foo(10);
    }
}&lt;/pre&gt;

&lt;p&gt;In this example, both overloads of Foo are applicable – the integer 10 is convertible to object, and is certainly convertible to int. The traditional tie breaker rule simply says that since 10 &lt;em&gt;is&lt;/em&gt; an integer, the conversion to int is better than the conversion to object. Therefore Foo(int x) wins. &lt;/p&gt;

&lt;h5&gt;Param arrays&lt;/h5&gt;

&lt;p&gt;Well what about parameter arrays? Lets tweak our example a little bit:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y) { }
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;params int&lt;/span&gt;[] x) { }

    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;C &lt;/span&gt;c = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C&lt;/span&gt;();
        c.Foo(10, 20);
    }
}&lt;/pre&gt;

&lt;p&gt;In this example, the compiler takes the params array, and expands it so that the signature looks like Foo(int x_1, int x_2), and uses that signature as the candidate signature. However, it also notes that the signature came from a params array, and param arrays are treated as second-class compared to real parameters. Since everything else is equal, the second-class-ness of the params array loses the tie to the first class parameters. &lt;/p&gt;

&lt;h5&gt;Optional arguments&lt;/h5&gt;

&lt;p&gt;So how do optionals play into the mix? Well lets consider this third example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;x) { }
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y = 0, &lt;span style="color: blue"&gt;int &lt;/span&gt;z = 10) { }

    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;C &lt;/span&gt;c = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C&lt;/span&gt;();
        c.Foo(10);
    }
}&lt;/pre&gt;

&lt;p&gt;In this example, both candidates are applicable, since they both match for the first argument, and the second candidate has optional arguments for the rest. The conversion for the first argument is the same for both candidates, and there are no param arrays. How do we pick which one is better?&lt;/p&gt;

&lt;p&gt;Much like param arrays, the thing to do here is to treat optional arguments like param arrays – as second class arguments. Because the first method has no optional arguments and the second one does, we pick the first one.&lt;/p&gt;

&lt;p&gt;Notice that if both candidates had optional arguments, at that point we’ve got nothing more to go on, and so the call is ambiguous.&lt;/p&gt;

&lt;h5&gt;&lt;/h5&gt;

&lt;h5&gt;Philosophy time!&lt;/h5&gt;

&lt;p&gt;Okay! Time for some philosophy. Why are we doing this feature now? Why didn’t we do it earlier?&lt;/p&gt;

&lt;p&gt;Let me tackle the second question first, as it’s got a shorter answer. The reason we didn’t do it earlier is that we really didn’t want this feature in our language. We’ve pushed back on it for this long because it’s not the paradigm that we want.&lt;/p&gt;

&lt;p&gt;So that brings us to the first question – why now?&lt;/p&gt;

&lt;p&gt;Well, the quick answer is because of COM. It just won’t go away! Try as we might, people still use it (and are still going to continue using it). What does COM have to do with C#? Office. Office PIAs. The Office PIAs are designed such that many of the methods have about 30 parameters, and all of them are optional. Most of the time, what you’d want to do is specify one argument, and use the defaults for the rest.&lt;/p&gt;

&lt;p&gt;Enter named and optional arguments. Because we allow you now to call methods without specifying their optional arguments, you can now call these Office methods without passing Type.Missing in as every other argument. And because we allow you to use names to specify exactly which parameter you’re passing this argument for, you can call the method by only passing what you want, and omitting the rest.&lt;/p&gt;

&lt;p&gt;Combine this with our COM-no-ref feature in which the compiler takes the optional ref parameter and generates a local temporary variable for you and passes that as the parameter, your COM code will look much cleaner and will be much less tedious to write.&lt;/p&gt;

&lt;p&gt;As I mentioned &lt;a href="http://blogs.msdn.com/samng/archive/2008/10/28/microsoft-visual-studio-2010.aspx"&gt;way back when&lt;/a&gt;, one of the big themes for C# 4.0 is interop with other “runtimes” – COM, dynamic languages, other user-defined type systems. That theme made this feature set a must-have. &lt;/p&gt;

&lt;p&gt;As always, I would love all the feedback you guys have! And of course, happy coding!&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2009%2f04%2f17%2fnamed-and-optional-arguments-ties-and-philosophies.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2009%2f04%2f17%2fnamed-and-optional-arguments-ties-and-philosophies.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9554683" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/samng/archive/tags/Overload+Resolution/default.aspx">Overload Resolution</category><category domain="http://blogs.msdn.com/samng/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/samng/archive/tags/Named+arguments/default.aspx">Named arguments</category><category domain="http://blogs.msdn.com/samng/archive/tags/Default+parameters/default.aspx">Default parameters</category><category domain="http://blogs.msdn.com/samng/archive/tags/Optional+arguments/default.aspx">Optional arguments</category></item><item><title>Named arguments and overload resolution</title><link>http://blogs.msdn.com/samng/archive/2009/04/01/named-arguments-and-overload-resolution.aspx</link><pubDate>Wed, 01 Apr 2009 21:16:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9527503</guid><dc:creator>samng</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/samng/comments/9527503.aspx</comments><wfw:commentRss>http://blogs.msdn.com/samng/commentrss.aspx?PostID=9527503</wfw:commentRss><description>&lt;p&gt;Last time we talked about the &lt;a href="http://blogs.msdn.com/samng/archive/2009/02/03/named-arguments-optional-arguments-and-default-values.aspx"&gt;basics of named arguments, optional arguments, and default values&lt;/a&gt;. From here on out, I’m just going to refer to the whole feature group as “named and optional arguments” – it’s just too much typing otherwise (we actually just refer to the feature as N&amp;amp;O internally). Let’s now dive a little deeper into how overload resolution works for the feature.&lt;/p&gt;  &lt;h5&gt;Where do we get those wonderful names?&lt;/h5&gt;  &lt;p&gt;The first thing we need to figure out, is where we get the names from. Because the CLR does not consider parameter names as part of the method signature, it is perfectly legal to override a method and specify different parameter names than the base method that one is overriding. Lets consider the following:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Animal
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public virtual void &lt;/span&gt;Eat(&lt;span style="color: blue"&gt;string &lt;/span&gt;foodType = &lt;span style="color: #a31515"&gt;&amp;quot;Grub&amp;quot;&lt;/span&gt;) { }
}

&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Monkey &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Animal
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Eat(&lt;span style="color: blue"&gt;string &lt;/span&gt;bananaType = &lt;span style="color: #a31515"&gt;&amp;quot;Green banana&amp;quot;&lt;/span&gt;) { }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is perfectly legal C# code. So then which names do we pick? Do we pick the ones from the base method? Or the most derived one? What about interfaces? Let’s consider a usage of named arguments with this example.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Program
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;Monkey &lt;/span&gt;m = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Monkey&lt;/span&gt;();
        &lt;span style="color: #2b91af"&gt;Animal &lt;/span&gt;a = m;

        m.Eat(bananaType:&lt;span style="color: #a31515"&gt;&amp;quot;Ripe banana&amp;quot;&lt;/span&gt;);
        a.Eat(foodType:&lt;span style="color: #a31515"&gt;&amp;quot;Yummy grub&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If we consider the receiver (aka the calling object/type) as the anchor in this whole scheme, and use its statically determined type to figure out the names, then we’ve got ourselves a nice little scheme that is deterministic and quite sensible.&lt;/p&gt;

&lt;p&gt;In our example then, because m is statically typed to be of type Monkey, m.Eat gets the names of the parameters from the Monkey class, and so bananaType is the correct name to be using. Similarly, a is typed Animal, and so the call to a.Eat gets the name foodType as its parameter. Notice that even though their runtime types will be identical (that is, we’ve taken an instance of Monkey and assigned it into an Animal local variable), that doesn’t matter – &lt;em&gt;the named parameter feature is simply a syntactic sugar for a compile time rewrite&lt;/em&gt;. &lt;/p&gt;

&lt;h5&gt;So what happens under the covers?&lt;/h5&gt;

&lt;p&gt;Lets take a look at the example that we used last time:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContactList
&lt;/span&gt;{
    &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;Contact&amp;gt; SearchForContacts(
        &lt;span style="color: blue"&gt;string &lt;/span&gt;name = &lt;span style="color: #a31515"&gt;&amp;quot;any&amp;quot;&lt;/span&gt;,
        &lt;span style="color: blue"&gt;int &lt;/span&gt;age = -1,
        &lt;span style="color: blue"&gt;string &lt;/span&gt;address = &lt;span style="color: #a31515"&gt;&amp;quot;any&amp;quot;&lt;/span&gt;) { ... }

    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;ContactList &lt;/span&gt;list = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContactList&lt;/span&gt;();
        &lt;span style="color: blue"&gt;var &lt;/span&gt;x = list.SearchForContacts(age:26);
    }
}&lt;/pre&gt;

&lt;p&gt;What actually happens under the covers here? When the compiler sees the call to list.SearchForContacts, it first performs a few quick validations to make sure that any positional arguments (arguments not specified by name) occur &lt;em&gt;before&lt;/em&gt; any named arguments, and that no names are specified twice. Then it generates a set of all applicable candidates. In our example, there is but one candidate to consider. Then for each candidate, the compiler looks at the arguments, and performs a few verifications.&lt;/p&gt;

&lt;p&gt;First it checks to make sure that the names specified in the call (in our case, “age”) is valid for the candidate (ie the candidate has a parameter named “age”). Next, the compiler moves past all positional arguments attempts to match each named argument up with its corresponding parameter. Of course, all named arguments must match parameters who do not have a positional argument specified for it (ie you cannot specify an argument for the same parameter twice).&lt;/p&gt;

&lt;p&gt;For each parameter that does not have a corresponding positional or named argument, the compiler checks to make sure it is optional. It then uses the default parameter value for each of those arguments.&lt;/p&gt;

&lt;p&gt;Once the compiler has generated this augmented argument list, it performs argument convertibility on it as usual. The resulting augmented list in our example then, is: “any”, 26, “any”.&lt;/p&gt;

&lt;h5&gt;Just compiler magic&lt;/h5&gt;

&lt;p&gt;I love magic. I love the concept of magic. It amazes me. I went to Disneyland recently for PDC, and it was &lt;em&gt;magical. &lt;/em&gt;I know it isn’t real, and that something else is happening, but I love it anyway. That’s what named arguments are like. Underneath the covers, there is no trace of named (or optional) things – it all looks like straight IL, as if you called the methods with the augmented argument list.&lt;/p&gt;

&lt;p&gt;That means that after the compiler has generated the augmented list for you and has found the best candidate, it treats the call as if you had called it with the augmented list, and everything else behaves as it used to.&lt;/p&gt;

&lt;p&gt;That being said, it means that this feature is &lt;em&gt;totally&lt;/em&gt; a compile time syntactic sugar, and so it doesn’t introduce any new dependencies, and doesn’t introduce any new compatibility issues or anything like that. Programs that you compile against one set of names and default values will absolutely continue to keep working even if the library changes and has a new set of names and default values. &lt;/p&gt;

&lt;h5&gt;One more piece of magic&lt;/h5&gt;

&lt;p&gt;Turns out there’s one more crucial thing that the compiler does for you, which is really worth mentioning. Order of evaluation. Lets consider the following example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;C &lt;/span&gt;c = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C&lt;/span&gt;();

        c.M(z:Foo(), x:Bar(), y:Baz());
    }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;M(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y, &lt;span style="color: blue"&gt;int &lt;/span&gt;z) { ... }
}&lt;/pre&gt;

&lt;p&gt;What happens here? Well, the compiler will reorder the arguments so that the result of Bar() gets passed as the first&amp;#160; argument, the result of Baz() gets passed as the second, and the result of Foo() gets passed as the third. However, what order should these sub-expressions be evaluated? You’d expect them to be evaluated as written – Foo first, then Bar, then Baz. &lt;/p&gt;

&lt;p&gt;And that’s exactly what we do. We essentially create temporaries which store the value of evaluating each of those sub-expressions (which means that all side effects of evaluating each expression happen in syntax order as you’d expect), and reorder those temporaries to match their respective named positions. See? Magic!&lt;/p&gt;

&lt;p&gt;So hopefully that gives you a good feel for how the feature works, and how overload resolution for it works. Have fun with it, and definitely give us your feedback!&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2009%2f04%2f01%2fnamed-arguments-and-overload-resolution.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2009%2f04%2f01%2fnamed-arguments-and-overload-resolution.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9527503" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/samng/archive/tags/Overload+Resolution/default.aspx">Overload Resolution</category><category domain="http://blogs.msdn.com/samng/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/samng/archive/tags/Named+arguments/default.aspx">Named arguments</category><category domain="http://blogs.msdn.com/samng/archive/tags/Default+parameters/default.aspx">Default parameters</category><category domain="http://blogs.msdn.com/samng/archive/tags/Optional+arguments/default.aspx">Optional arguments</category></item><item><title>Named arguments, optional arguments, and default values</title><link>http://blogs.msdn.com/samng/archive/2009/02/03/named-arguments-optional-arguments-and-default-values.aspx</link><pubDate>Wed, 04 Feb 2009 09:33:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9394792</guid><dc:creator>samng</dc:creator><slash:comments>29</slash:comments><comments>http://blogs.msdn.com/samng/comments/9394792.aspx</comments><wfw:commentRss>http://blogs.msdn.com/samng/commentrss.aspx?PostID=9394792</wfw:commentRss><description>&lt;p&gt;C# 4.0 introduces the concept of optional parameter values into the language. Now, this has been a controversial subject in the past, and we have had many requests for the feature, but have traditionally stayed away from it. So, why now?&lt;/p&gt;  &lt;p&gt;Well, before we get into the philosophy of why we decided to add it this time (which we will! I promise!), first lets discuss the feature itself.&lt;/p&gt;  &lt;p&gt;I'll first let's talk about the features themselves, and will follow that with a brief discussion about the focus of C# 4.0 and how these features align with that focus. I'll expand in depth as to how each feature works, and what the compiler does behind the scenes in future posts.&lt;/p&gt;  &lt;h5&gt;Default parameter values&lt;/h5&gt;  &lt;p&gt;We've all had the experience of writing (or at least using) methods whose parameters are optional in nature. Often, those methods will contain overloads which will have the optional parameters removed, and will simply be a wrapper for the actual method, passing the default values that the library writer wants to provide for the method.&lt;/p&gt;  &lt;p&gt;Default parameter values give us a way to explicitly declare the default value that we would like to be used for the parameter in the method signature itself, instead of in the forwarding method. This gives the added benefit of allowing the IDE to help out and inform the consumer of the default value for the given parameter.&lt;/p&gt;  &lt;p&gt;These guys are specified in one of two ways:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Foo([&lt;span style="color: #2b91af"&gt;DefaultParameterValueAttribute&lt;/span&gt;(10)] &lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y = 20) { }
}&lt;/pre&gt;

&lt;p&gt;The first mechanism is &lt;em&gt;purely &lt;/em&gt;a backwards compatibility issue and an interop issue with COM and VB. We highly recommend against using this method, however, because it specifies that there is a default parameter value for the parameter, but does not specify that the parameter is in fact optional. This means that the default value given will never be used. &lt;/p&gt;

&lt;p&gt;The second mechanism really says two things - first, it tells the compiler that this parameter is optional, meaning the user does not have to specify an argument in this position. Second, it gives the compiler the value to use when the user does not specify an argument for the parameter.&lt;/p&gt;

&lt;h5&gt;How default parameter values are encoded&lt;/h5&gt;

&lt;p&gt;Because the new syntax really does two things, it is encoded in the metadata as two things. We encode both the optional flag in the metadata signature (which is equivalent to using the OptionalAttribute attribute), and we encode the default value as the DefaultParmeterValueAttribute attribute. &lt;/p&gt;

&lt;p&gt;These two mechanisms have already existed in the CLR, and are in fact what the VB compiler produces today, giving us an easy choice for how to encode the feature.&lt;/p&gt;

&lt;p&gt;Note that the compiler will give you an error if you try to specify both a DefaultParameterValueAttribute attribute as well as a default value using the new syntax. &lt;/p&gt;

&lt;p&gt;Also note that the compiler enforces that all optional parameters using the new syntax are specified &lt;em&gt;after&lt;/em&gt; all required parameters.&lt;/p&gt;

&lt;p&gt;Lastly, the compiler will not allow you to specify default parameter values for ref or out parameters. This is because there is no valid constant expression that is convertible to the ref or out type.&lt;/p&gt;

&lt;h5&gt;Optional arguments - how default parameter values are used&lt;/h5&gt;

&lt;p&gt;In order to make use of the default parameter values, we've added a feature which allows the caller to omit arguments for parameters which have default parameter values specified.&lt;/p&gt;

&lt;p&gt;Notice that because we enforce that optional parameter values are specified at the end of the parameter list, you cannot omit an argument but specify an argument for a later position.&lt;/p&gt;

&lt;p&gt;This means that even though existing libraries may have specified the DefaultParameterValueAttribute and the OptionalAttribute for a parameter in the middle of the list, the C# compiler will not allow you to call that method without specifying a value for that parameter.&lt;/p&gt;

&lt;p&gt;You can kind of think of optional arguments as a params array - the compiler will allow you to call the method without specifying the arguments, but they must be at the end of the parameter list.&lt;/p&gt;

&lt;h5&gt;What gets code gen'ed?&lt;/h5&gt;

&lt;p&gt;First we need to note that the use of optional arguments is really just syntactic sugar. The compiler cannot generate a call without actually providing all the arguments - it simply provides an argument for you and allows you to omit specifying it.&lt;/p&gt;

&lt;p&gt;Once the compiler realizes that you're calling a method and omitting an argument because it is optional, it takes the value specified in the DefaultParameterValueAttribute and encodes that as a constant value for the argument that you've omitted. Note that if you're calling a library that doesn't have a default value specified but is still optional, the compiler will use default(T) as the value, where T is the type of the parameter. Also note that for COM calls, we also allow you to omit arguments to ref parameters by generating the temporaries for you.&lt;/p&gt;

&lt;h5&gt;Putting it all together - Named arguments&lt;/h5&gt;

&lt;p&gt;The ability to omit specifying arguments for parameters that have a default value is really taken advantage of by the Named arguments feature. This feature allows you to specify by name the parameter for which you are providing a value. This means that you can now omit specifying arguments for all parameters, and only specify the ones which you actually care about.&lt;/p&gt;

&lt;p&gt;For COM programmers, this is like heaven! I recently &amp;quot;got&amp;quot; to play with Office interop a bit, and found that the average method had 30 parameters! Most of the parameters are wonderfully optional, and with the ability to now omit them, code looks much cleaner and is much more readable.&lt;/p&gt;

&lt;p&gt;Sorry, I'm getting ahead of myself. First let me describe the feature.&lt;/p&gt;

&lt;h5&gt;Named argument usage&lt;/h5&gt;

&lt;p&gt;This feature introduces new syntax at the call site of a method. Consider the following example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContactList
&lt;/span&gt;{
    &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Contact&lt;/span&gt;&amp;gt; SearchForContacts(
        &lt;span style="color: blue"&gt;string &lt;/span&gt;name = &lt;span style="color: #a31515"&gt;&amp;quot;any&amp;quot;&lt;/span&gt;,
        &lt;span style="color: blue"&gt;int &lt;/span&gt;age = -1,
        &lt;span style="color: blue"&gt;string &lt;/span&gt;address = &lt;span style="color: #a31515"&gt;&amp;quot;any&amp;quot;&lt;/span&gt;) { ... }

    &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main()
    {
        &lt;span style="color: #2b91af"&gt;ContactList &lt;/span&gt;list = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContactList&lt;/span&gt;();
        var x = list.SearchForContacts(age:26);
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We've got some library method that searches through some contacts and finds the ones that match our criteria (I know, its a horrible API, but bear with me). The library is great! ;) It specifies the default wildcards for us so that we can omit them as we wish. Now suppose we want to find all contacts aged 26.&lt;/p&gt;

&lt;p&gt;Well, since we now have optional arguments, we can omit the arguments &lt;em&gt;after&lt;/em&gt; the age parameter, but that gets us half way there. Named arguments get us the rest of the way.&lt;/p&gt;

&lt;p&gt;By using the new syntax, we can specify by name the argument that we want to specify a value for, and omit the rest. The syntax is quite simple (well, not really, but I'll get into the details of its complexities later): simply specify the name of the parameter that you want to specify an argument for, add a colon, then add the value that you want.&lt;/p&gt;

&lt;p&gt;Note that the value can be any expression type that you normally could have specified. Note also that named arguments are &lt;em&gt;not&lt;/em&gt; restricted to parameters that are optional or have default values. Lastly, note that you don't even have to specify the arguments in order! We could call our method with the following:&lt;/p&gt;

&lt;pre class="code"&gt;list.SearchForContacts(address:&lt;span style="color: #a31515"&gt;&amp;quot;home&amp;quot;&lt;/span&gt;, name:&lt;span style="color: #a31515"&gt;&amp;quot;sam&amp;quot;&lt;/span&gt;, age:30);&lt;/pre&gt;

&lt;p&gt;The arguments don't have to be in any particular order. The only rule is that the named arguments must be specified &lt;em&gt;at the end&lt;/em&gt; of the argument list. This means that all positional arguments (the &amp;quot;normal&amp;quot; ones that aren't specified by name) must be given first. Once the compiler encounters a named specification, it will produce an error upon encountering any further unnamed arguments.&lt;/p&gt;

&lt;p&gt;So that's the feature in a nutshell. There are more details that I'll get to later, but in the mean time I'd love to get your feedback on the feature, on its uses, and on how we've chosen to design it.&lt;/p&gt;

&lt;p&gt;And as always, happy coding!&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2009%2f02%2f03%2fnamed-arguments-optional-arguments-and-default-values.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fsamng%2farchive%2f2009%2f02%2f03%2fnamed-arguments-optional-arguments-and-default-values.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9394792" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/samng/archive/tags/Overload+Resolution/default.aspx">Overload Resolution</category><category domain="http://blogs.msdn.com/samng/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/samng/archive/tags/Named+arguments/default.aspx">Named arguments</category><category domain="http://blogs.msdn.com/samng/archive/tags/Default+parameters/default.aspx">Default parameters</category><category domain="http://blogs.msdn.com/samng/archive/tags/Optional+arguments/default.aspx">Optional arguments</category></item></channel></rss>