vivek nuna vivek nuna - 24 days ago 17
C# Question

How many string objects are created by below code?

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


I have been these options to answer this question.


  1. 1

  2. 11

  3. 10

  4. 2



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

I have a doubt, Is
string s = "";
creates no object. I dont think so, Please make me clear.

If I append string with + operator, it creates new string, so I think It will be a new object created in every iteration of for loop.

So I think there will be 11 objects created. Let me know If I'm incorrect.

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 below link, but still its not clear.

Link1

Please cover
string str
and
string str = null
case too. What happens If we dont initialize string and when If I assign string to null. So It will be an object or no object in these two cases.

string str;

string str = null;


Later in the code, If I do.

str = "abc";


Is there any programming way to calculate number of objects?, because I think It may by a debatable topic. How can I be 100 % by doing some programming or by some tool? I cannot see this in IL code.

I have tried the below code,just to make sure whether new object is created or not. It writes 'different' for each iteration. It means it always gives me a different object, So there can a possibility of 10 or 20 objects. because it does not give me info of intermediate state(boxing 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'm not agreed by the statement that
string str = ""
does not create any object. I tried this practically.

string s = null;
object obj = null;

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


Code writes "Same", but If I write
string s = "";
, It writes "Different" on console.

Answer

Well, let's 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.ToString() - first object to create
  // s + i.ToString() - second object to create
  s = s + i;
}

To test that string s = "" doesn't 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. In case of

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

as you can see the result can be and (will be) computed at the compile time, so just one object ("1234") will be created. In case of

string str; // just a declaration, str contains trash

string str = null; // no objects created
...
str = "abc"; // an object ("abc") created
Comments