Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How int + string becomes string?

Tags:

I came across a strange way to implement ToString() and I am wondering how it works:

public string tostr(int n)  {     string s = "";     foreach (char c in n-- + "") {  //<------HOW IS THIS POSSIBLE ?         s = s + c;     }     return s; } 

Is the iterator assuming the size of a char?

like image 399
Nabil Lamriben Avatar asked Jan 30 '17 13:01

Nabil Lamriben


People also ask

How do you convert int to string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.

How do I convert an int to a string in C++?

The next method in this list to convert int to string in C++ is by using the to_string() function. This function is used to convert not only the integer but numerical values of any data type into a string. The to_string() method is included in the header file of the class string, i.e., <string> or <cstring>.


2 Answers

It calls the String.Concat(object, object) method implicitly, which concatenates the string representations of two specified objects:

string result = String.Concat("", n--); 

The String.Concat(object, object) method then calls String.Concat(string, string). To read the Concat's source and check it in depth, first go here: String.cs source code in C# .NET and then in that page in the search TextBox type String and then click on the String.cs link in the results to go to the String.cs source code in C# .NET page and check the Concat method.

This is the method definition:

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

As you see this calls public static String Concat(String str0, String str1) method finally:

public static String Concat(String str0, String str1)  {     Contract.Ensures(Contract.Result<string>() != null);     Contract.Ensures(Contract.Result<string>().Length ==         (str0 == null ? 0 : str0.Length) +          (str1 == null ? 0 : str1.Length));     Contract.EndContractBlock();       if (IsNullOrEmpty(str0)) {         if (IsNullOrEmpty(str1)) {              return String.Empty;         }         return str1;     }       if (IsNullOrEmpty(str1)) {          return str0;      }      int str0Length = str0.Length;      String result = FastAllocateString(str0Length + str1.Length);      FillStringChecked(result, 0,        str0);     FillStringChecked(result, str0Length, str1);       return result; } 

And this is the underlying IL, by Ildasm:

.method public hidebysig instance string          tostr(int32 n) cil managed {   // Code size       74 (0x4a)   .maxstack  3   .locals init ([0] string s,            [1] string V_1,            [2] int32 V_2,            [3] char c,            [4] string V_4)   IL_0000:  nop   IL_0001:  ldstr      ""   IL_0006:  stloc.0   IL_0007:  nop   IL_0008:  ldarg.1   IL_0009:  dup   IL_000a:  ldc.i4.1   IL_000b:  sub   IL_000c:  starg.s    n   IL_000e:  box        [mscorlib]System.Int32   IL_0013:  call       string [mscorlib]System.String::Concat(object)   IL_0018:  stloc.1   IL_0019:  ldc.i4.0   IL_001a:  stloc.2   IL_001b:  br.s       IL_0039   IL_001d:  ldloc.1   IL_001e:  ldloc.2   IL_001f:  callvirt   instance char [mscorlib]System.String::get_Chars(int32)   IL_0024:  stloc.3   IL_0025:  nop   IL_0026:  ldloc.0   IL_0027:  ldloca.s   c   IL_0029:  call       instance string [mscorlib]System.Char::ToString()   IL_002e:  call       string [mscorlib]System.String::Concat(string,                                                               string)   IL_0033:  stloc.0   IL_0034:  nop   IL_0035:  ldloc.2   IL_0036:  ldc.i4.1   IL_0037:  add   IL_0038:  stloc.2   IL_0039:  ldloc.2   IL_003a:  ldloc.1   IL_003b:  callvirt   instance int32 [mscorlib]System.String::get_Length()   IL_0040:  blt.s      IL_001d   IL_0042:  ldloc.0   IL_0043:  stloc.s    V_4   IL_0045:  br.s       IL_0047   IL_0047:  ldloc.s    V_4   IL_0049:  ret }// end of method tostr 
like image 77
Salah Akbari Avatar answered Oct 08 '22 08:10

Salah Akbari


Explaining this "step by step" :

// assume the input is 1337 public string tostr(int n) {     //line below is creating a placeholder for the result string     string s = "";     // below line we can split into 2 lines to explain in more detail:     // foreach (char c in n-- + "") {     // then value of n is concatenated with an empty string :     // string numberString = n-- + ""; // numberString is "1337";     // and after this line value of n will be 1336     // which then is iterated though :     // foreach(char c in numberString) { // meaning foreach(char c in "1337")     foreach (char c in n-- + "") {  //<------HOW IS THIS POSSIBLE ?         s = s + c; // here each sign of the numberString is added into the placeholder     }     return s; // return filled placeholder } 

So basically if you concatenate string with int it will automatically call int.ToString method and join the string together.

like image 32
Mateusz Avatar answered Oct 08 '22 10:10

Mateusz