stringOperand + null = ?

  • Thread starter Thread starter Mark Chambers
  • Start date Start date
M

Mark Chambers

Hi there,

In the expression A + B where A is a string and B isn't, I know that
"B.ToString()" will be called and the results concatenated. If B is null
however does the standard guarantee that "String.Empty" will be used
instead. Thanks.
 
Mark said:
Hi there,

In the expression A + B where A is a string and B isn't, I know that
"B.ToString()" will be called and the results concatenated. If B is null
however does the standard guarantee that "String.Empty" will be used
instead. Thanks.

The + operator uses the String.Concat method.

From the specification of the String.Concat method:

"An Empty string is used in place of any null argument."
 
Hi there,
The + operator uses the String.Concat method.

From the specification of the String.Concat method:

"An Empty string is used in place of any null argument."

Thanks. I didn't know it defers to "String.Concat()".
 
Mark Chambers said:
Hi there,

In the expression A + B where A is a string and B isn't, I know that
"B.ToString()" will be called and the results concatenated. If B is null
however does the standard guarantee that "String.Empty" will be used
instead. Thanks.

Yup, the spec says:

<quote>
If an operand of string concatenation is null, an empty string is
substituted. Otherwise, any non-string argument is converted to its
string representation by invoking the virtual ToString method inherited
from type object. If ToString returns null, an empty string is
substituted.
</quote>
 
Yup, the spec says:
<quote>
If an operand of string concatenation is null, an empty string is
substituted. Otherwise, any non-string argument is converted to its
string representation by invoking the virtual ToString method inherited
from type object. If ToString returns null, an empty string is
substituted.
</quote>

Thanks for the confirmation. Appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top