c# - 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 answer question.

  1. 1
  2. 11
  3. 10
  4. 2

i have simple code, want know how many string objects created code.

i have doubt, string s = ""; creates no object. dont think so, please make me clear.

if append string + operator, creates new string, think new object created in every iteration of loop.

so think there 11 objects created. let me know if i'm incorrect.

string result = "1" + "2" + "3" + "4";  //compiler optimise code below line. string result = "1234"; //so in case 1 object created?? 

i followed below link, still not clear.

link1

please cover string str , string str = null case too. happens if dont initialize string , when if assign string null. object or no object in these 2 cases.

string str;  string str = null; 

later in code, if do.

str = "abc"; 

is there programming way calculate number of objects?, because think may debatable topic. how can 100 % doing programming or tool? cannot see in il code.

i have tried below code,just make sure whether new object created or not. writes 'different' each iteration. means gives me different object, there can possibility of 10 or 20 objects. because not give me info of intermediate state(boxing i when doing s = s + i)

    string s = "0";     object obj = s;     (int = 0; < 10; i++)     {         s = s + i;          if (object.referenceequals(s, obj))         {             console.write("same");         }         else         {             console.write("different");         }     } 

i'm not agreed statement string str = "" not create object. tried practically.

    string s = null;     object obj = null;      if (object.referenceequals(s, obj))     {         console.write("same");     }     else     {         console.write("different");     } 

code writes "same", if write string s = "";, writes "different" on console.

i have 1 more doubt now.

what difference between s = s + i , 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 whats difference between box , instance here

well, let's count:

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

to test string s = "" doesn't create additional object can put

string s = "";  if (object.referenceequals(s, string.empty))   console.write("empty string has been cached"); 

finally, have 20 objects: 0 + 10 * 2 (10 boxed ints , 10 strings). in case of

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

as can see result can , (will be) computed @ compile time, 1 object ("1234") created. in case of

string str; // declaration, str contains trash  string str = null; // no objects created ... str = "abc"; // object ("abc") created 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -