How does string.Format handle null values?

后端 未结 4 1350
感情败类
感情败类 2021-02-11 12:07

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

4条回答
  •  别跟我提以往
    2021-02-11 12:35

    The first call gets resolved as a call to Format(object), while the second gets resolved as a call to Format(object[]). Null parameters are handled differently by these different overloads.

    Overload resolution is described here. The relevant part is that for the second call to Format, an overload of Format(params object[]) gets expanded to Format(object[]), which is preferred to Format(object). The literal null is both an object[] and an object, but object[] is more specific, so that is chosen.

提交回复
热议问题