Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:23:21+00:00 2026-06-15T22:23:21+00:00

If I run this code: Console.WriteLine( String.Format( {0}, null ) ); I get a

  • 0

If I run this code:

Console.WriteLine( String.Format( "{0}", null ) );

I get a ArgumentNullException but if I run this code:

String str = null;
Console.WriteLine( String.Format( "{0}", str ) );

it runs just fine and the output is an empty string.

Now the two piece look equivalent to me – they both pass a null reference into String.Format() yet the behavior is different.

How id different behavior possible here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:23:22+00:00Added an answer on June 15, 2026 at 10:23 pm

    Just decompile the code to work out what’s going on.

    string.Format("{0}", null)
    

    calls the most specific applicable overload, which is string.Format(string, object[]).

    The overloads of string.Format are:

    Format(String, Object)
    Format(String, Object[])
    Format(IFormatProvider, String, Object[])
    Format(String, Object, Object)
    Format(String, Object, Object, Object)
    

    Hopefully it’s obvious why the last three options are invalid.

    To work out which of the first two to use, the compiler compares the conversion from null to Object to the conversion from null to Object[]. The conversion to Object[] is deemed “better” because there’s a conversion from Object[] to Object, but not vice versa. This is the same logic by which if we had:

    Foo(String)
    Foo(Object)
    

    and called Foo(null), it would pick Foo(String).

    So your original code is equivalent to:

    object[] values = null;
    string.Format("{0}", values);
    

    At this point, hopefully you’d expect an ArgumentNullException – as per the documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tryed to run this code in my console: script/plugin install git://github.com/apotonick/cells.git ...but
I'm trying to run this code but this error appear: Uncaught TypeError: string is
When I run this code to call the Google API, all I get is
When I run this code, About half-way through the concatenation loop, $xml becomes null
Consider this code sample: public class Human { public string Value { get; set;}
if I run this C# code int realInt = 3; string foo = bar;
Am trying to run a selenium suite using this code which runs perfectly on
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run this code: - (void)unitButtonButtonTapped:(id)sender { [_label setString:@Last button: Unembossed square]; MilitaryUnits *target
Why this code don't work,when i want run this code vwd 2008 express show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.