How does string.Format handle null values?

后端 未结 3 802
野性不改
野性不改 2021-02-11 11:45

In the following code below, why do the two string.Format calls not behave the same way? In the first one, no exception is thrown, but in the second one an Ar

3条回答
  •  抹茶落季
    2021-02-11 12:32

    In your first example, you are hitting Format(String, Object), which looks like this when disassembled:

     public static string Format(string format, object arg0)
     {
        return Format(null, format, new object[] { arg0 });
     }
    

    Note the new object[] around that.

    The second one, you are apparently hitting the Format(string, object[]) usage, at least that is the one being invoked when I perform the same test. Disassembled, that looks like this:

     public static string Format(string format, params object[] args)
     {
         return Format(null, format, args);
     }
    

    So all of these actually get funneled to Format(IFormatProvider, string, object[]). Cool, let's look at the first few lines there:

    public static string Format(IFormatProvider provider, string format, params object[] args)
    {
        if ((format == null) || (args == null))
        {
            throw new ArgumentNullException((format == null) ? "format" : "args");
        }
    ...
    }
    

    ...welp, there's your problem, right there! The first invocation is wrapping it in a new array, so it's not null. Passing in null explicitly doesn't make it do that, due to the specific instance of Format() that's calling.

提交回复
热议问题