Int for string without explicit conversion?

I have a simple question, but I'm surprised.

This code works:

int itemID = 1; string dirPath = @"C:\" + itemID + @"\abc"; 

Why don't I need to do itemID.ToString() in this case?

+6
source share
6 answers

from MSDN

The binary + operator concatenates strings when one or both operands have a string of type. If the string concatenation operand is NULL, the empty string is replaced. Otherwise, any non-string argument is converted to its string representation, invoking the ToString virtual method inherited from the type object. If ToString returns null, an empty string will be replaced.

Turning around on my answer a bit, based on some comments in other answers ...

This process is not just โ€œsyntactic sugarโ€ or convenience. This is the result of a core C # language feature called operator overloading. In the case of "Operator +" and "String + overload", overload is provided as a means of abstracting the internal elements of the String class, which is a foundation based on good design principles. + String Overload ensures type safety by ensuring that it never returns a null value, instead returns an empty string for any operand that cannot be converted using the .ToString() method. However, even custom complex types (and not just primitives) can be added to the string, assuming they have an overload of .ToString() , without an implementation of the String type, knowing any different ones.

Operator overloading is the main function of a language in which more people must learn to use force.

+13
source

+ in string concatenation it is converted to a call to string.Concat , which internally calls ToString for each object without parameters.

string.Concate Method - MSDN

The method combines each object in args by calling the targetless ToString method of this object; he does not add any delimiters.

EDIT:

This is how it looks in ILSpy enter image description here

+8
source

The + operator has a number of overloads. Three of them are as follows:

operator + (line a, line b)

operator + (line a, object b)

operator + (object a, line b)

The code uses the second operator overload.

This operator, after determining as a match, converts it into a call to string.Concat , which can take any number of objects (of type object ) as its parameters.

In the definition of string.Concat it will call ToString for all parameters (after it first checks them first) to get their string value, and that is what will be executed.

Because of all this, you can always concatenate any object with a string, and it will compile and execute using this ToString method.

+6
source

Because it is done automatically by the compiler.

Basically:

  string a = "abc" + 1; 

compiled for:

  string a = string.Concat((object)"abc", (object)1); 

It is just syntactic sugar. Although I personally would not prefer an automatic conversion.

Link for Concat : http://msdn.microsoft.com/en-us/library/kbseaaft.aspx

+1
source

This is a language feature. ToString() is called inside objects when performing string concatenation using the + operator.

The main reason for this is the simple usability (and possibly increased readability due to smaller ToString() calls, but it can also fool you into thinking that an object is a string, when in fact it can be anything) for the developer.

For C #, Java behavior was probably adopted, which is identical. It may be interesting to ask why this was introduced in Java in the first place.

0
source

Very interesting question.

Let's look at your example IL code:

  ldstr "C:\\" ldloc.0 box int ldstr "\\abc" call string System.String.Concat(object, object, object) 

The called function behind is System.String.Concat, which accepts 3 objects!

Let's delve into the .NET source code:

 public static String Concat(Object arg0, Object arg1, Object arg2) { Contract.Ensures(Contract.Result<String>() != null); Contract.EndContractBlock(); if (arg0 == null) { arg0 = String.Empty; } if (arg1==null) { arg1 = String.Empty; } if (arg2==null) { arg2 = String.Empty; } return Concat(arg0.ToString(), arg1.ToString(), arg2.ToString()); } 

HOW YOU SEE ToString() , it will be called, but later! It's very impressive what the guys at Microsoft are doing!

0
source

Source: https://habr.com/ru/post/953575/


All Articles