And the Winner is..


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Looks like href="http://dotnetweblogs.com/dstone">David Stone one out by under a
minute… If href="mailto:john%20dot%20st%20dot%20clairAT%20NOSPAMreaktorgruppen%20dot%20no">John
St. Clair had only hit “add” a little faster… "urn:schemas-microsoft-com:office:office" />


"urn:schemas:contacts" />David and
John both hit the nail on the head.. here is
David’s response:


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"> class=commentbody> style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"> style="mso-list: Ignore">Ø style="FONT: 7pt 'Times New Roman'">     
No… because in the second
Format method, the format param is “{0}” and the args is an Object[].


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"> style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"> style="mso-list: Ignore">Ø style="FONT: 7pt 'Times New Roman'">     
Thus the args parameter is not null
in and of itself. It has a null string in the first slot, but the first method
just gave it an Object[], so it won’t throw any exception.


= "urn:schemas-microsoft-com:office:smarttags"
/> class=permalink>Kevin
Dente
class=permalink> tried it out and said it didn’t throw… Hmm… I just checked it
and on my machine it doesn’t throw, here is my code:


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 1">    style="COLOR: blue">static void
style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">Main style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">( style="COLOR: blue">string[] args)


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 1">    {


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 2">       style="COLOR: blue">string firstName = style="COLOR: blue">null;


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 2">      
Console.WriteLine (String.Format (“{0}”,
firstName));


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="mso-tab-count: 2">             
Console.ReadLine();
style="mso-tab-count: 1">       } style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">


href="http://dotnetguy.techieswithcats.com/">Brad, Guy of .NET makes a valid
point that we generally treat null and String.Empty to be the samething.. true,
true, and I encourage others to do the same but I don’t see how that effects
this code..


style="mso-ansi-language: PL">..brad


 

Comments (4)

  1. I was just expounding on the earlier explanation that the code you showed didn’t throw an exception, but that didn’t mean that one wasn’t eventually thrown, so I spelunked the code to see if it would or not.

  2. Kevin Dente says:

    Actually, I said that it DOES throw. But it turns out I was trying something a little different – String f = String.Format("{0}", null); And I just realized why it throws – the method is overloaded, and it ends up calling the Format(string format, params object[] args) overload in this case (which has the null check), rather than the Format(string format, object arg0) overload. On the other hand, if I call String f = String.Format("{0}", (string) null); – it doesn’t throw. This is actually quite interesting – how does the compiler choose which overloaded version to call here?

  3. It’s a matter of matching the parameters. string is not object[]. so it starts the params collection (string is object, so it’s a valid parameter for the collection). If you pass an object[] directly (for which a naked null would suffice), then what you pass is the array.

    Had you typed:

    String f = String.Format("{0}", null, null);

    you’d be back to not throwing (because the only way to satisfy the parameter list is by considering the two nulls to be objects).

    Parameter matching is already a fairly confusing thing, especially in the face of overloads. It’s no wonder they eliminated default values on parameters, because the complexity goes up many-fold with them.

  4. Kevin Dente says:

    Yeah, but why does null match the object[] param rather than the string param? Undoubtedly there are precedence rules for parameter matching in some spec somewhere – maybe I’ll just look them up.