How many string objects are created under the code?

string s = ""; for(int i=0;i<10;i++) { s = s + i; } 

I was this option to answer this question.

  • one
  • eleven
  • ten
  • 2

I have this simple code, I just want to know how many string objects this code will create.

I have a doubt, Is string s = ""; does not create any object. I don’t think so. Please let me understand.

If I add a line with the + operator, it creates a new line, so I think it will be a new object created at each iteration of the for loop.

So, I think that 11 objects will be created. Let me know if I'm wrong.

 String result = "1" + "2" + "3" + "4"; //Compiler will optimise this code to the below line. String result = "1234"; //So in this case only 1 object will be created?? 

I followed the link below, but is still unclear.

Link1

Please attach string str and string str = null case. What happens if we do not initialize the string and when If we assign the string to a null value. Thus, in these two cases, it will be an object or an object.

 string str; string str = null; 

Later in code if I do this.

 str = "abc"; 

Is there any programming way to calculate the number of objects ?, because I think this could be a debatable topic. How can I be 100% busy with programming or some kind of tool? I do not see this in IL code.

I tried the code below to make sure the new object is created or not. He writes "different" for each iteration. This means that it always gives me another object, so there may be a possibility of 10 or 20 objects. because it does not give me information about the intermediate state (box for i when doing s = s + i )

  string s = "0"; object obj = s; for (int i = 0; i < 10; i++) { s = s + i; if (Object.ReferenceEquals(s, obj)) { Console.Write("Same"); } else { Console.Write("Different"); } } 

I do not agree with the statement that string str = "" does not create any object. I tried it in practice.

  string s = null; object obj = null; if (Object.ReferenceEquals(s, obj)) { Console.Write("Same"); } else { Console.Write("Different"); } 

The code writes "Same", but if I write string s = ""; , he writes "Console" to the console.

I now have one more doubt.

what's the difference between s = s + i and s = s + i.ToString() .

s = s + i.ToString() IL code

 IL_000f: call instance string [mscorlib]System.Int32::ToString() IL_0014: call string [mscorlib]System.String::Concat(string, string) 

s = s + i IL code

 IL_000e: box [mscorlib]System.Int32 IL_0013: call string [mscorlib]System.String::Concat(object, object) 

So what is the difference between field and instance here

+6
source share
2 answers

Well, let count:

 string s = ""; // no new objects created, s assigned to string.Empty from the cache // 10 times: for(int i = 0; i < 10; i++) { // i <- first object to create (boxing): (object) i // s + i <- second object to create: string.Concat(s, (object) i); s = s + i; } 

To verify that string s = "" does not create an additional object, you can put

 string s = ""; if (object.ReferenceEquals(s, string.Empty)) Console.Write("Empty string has been cached"); 

Finally, we have 20 objects: 0 + 10 * 2 ( 10 in an int box and 10 string s). When

 string result = "1" + "2" + "3" + "4"; 

as you can see, result can and will be calculated at compile time, so only one object will be created ( "1234" ). When

 string str; // just a declaration, str contains trash string str = null; // no objects created ... str = "abc"; // an object ("abc") created 
+6
source

Although Dmitry Bychenko answers correctly and is well explained, but I want to add something.

 string s = ""; for(int i=0;i<10;i++) { s = s + i; } 

The code will give you 20 objects. The object will not be created for string str = "" because the link will have a cached empty string. And s = s + i; will be spent on the next IL code, so providing it will ensure that boxing occurs and a new object is created to refer to the new line s + i

 IL_000e: box [mscorlib]System.Int32 IL_0013: call string [mscorlib]System.String::Concat(object, object) 

You can see the IL code using IL Disassembler .

0
source

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


All Articles