Quick C# Quiz


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'">Will
this code throw an AgrumentNullException? and why?


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'">string
firstName = null;
String.Format (“{0}”, firstName);  style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> prefix = o ns = "urn:schemas-microsoft-com:office:office"
/> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'">Here
is the for code String.Format , if that helps…


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> 


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'">public
static String Format(String format, Object arg0) {


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">           
return Format(null, format, new
Object[] {arg0});


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">       
}


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> 


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'">public
static String Format( IFormatProvider
provider, String format, params Object[] args) { style="COLOR: red">


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">           
if (format == null || args == null)


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">               
throw new
ArgumentNullException((format==null)?”format”:”args”); style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'">


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">           
StringBuilder sb = new StringBuilder(format.Length + args.Length *
8);


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">           
sb.AppendFormat(provider,format,args);


style="MARGIN: 0in 0in 0pt 39pt; mso-layout-grid-align: none"> style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">           
return sb.ToString();


style="FONT-SIZE: 8.5pt; FONT-FAMILY: 'MS Sans Serif'; mso-bidi-font-family: 'MS Sans Serif'"> style="mso-spacerun: yes">       
} style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


 

Comments (7)

  1. David Stone says:

    No… because in the second Format method, the format param is "{0}" and the args is an Object[]. 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.

  2. John St. Clair says:

    Man, am I the first? First post 😉 — always wanted to say that.

    No, it will not throw an exception, because the test only checks whether the array itself (or the format string) is null, not whether the contents of the array are themselves null. The direct caller creates a new array of Objects, so it is logically impossible that args could ever be null (excluding EE exceptions, etc.).

  3. John St. Clair says:

    argghhhhhh

  4. Kevin Dente says:

    Yeah, but it DOES throw an exception. I tried it. But if that’s really the code for String.Format, then I don’t see why.

  5. Whether an exception is thrown or not is irrelevant to the code shown, because none of the code actually tests the arguments for null (only the array of arguments).

    A peek through StringBuilder.AppendFormat using Anakrino shows this just prior to the append:

    if (local12 == null)
    local12 = String.Empty;

    That’s why it doesn’t throw. StringBuilder turns null arguments into empty strings.

  6. kitsanaa says:

    good