Timeline for How many string objects are created by below code?
Current License: CC BY-SA 3.0
23 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 10, 2016 at 14:37 | comment | added | Lasse V. Karlsen |
The difference between the two calls to String.Concat is that in the .Object version both parameters will be converted to strings through a call to .ToString(), since one of the two objects is already a string, its ToString method will simply return this, in which case the actual number of strings won't differ at all. On the other hand, for the variant which calls .ToString on the int on the outside, no boxing of the int will be done, so same number of strings, one less object created.
|
|
| Nov 10, 2016 at 14:36 | comment | added | Lasse V. Karlsen | Just consider whether you're invalidating any existing answers here because nobody likes posting answers for a moving-target-question. | |
| Nov 10, 2016 at 13:58 | comment | added | Vivek Nuna | @LasseV.Karlsen any suggestions on my latest update in question ? | |
| Nov 10, 2016 at 13:51 | history | edited | Dmitrii Bychenko | CC BY-SA 3.0 |
added 39 characters in body
|
| Nov 10, 2016 at 13:50 | comment | added | Dmitrii Bychenko |
@vivek nuna: it will be 20 objects, with 10 string and 10 boxed ints among them
|
|
| Nov 10, 2016 at 13:49 | comment | added | Vivek Nuna | @DmitryBychenko, so it will be 10 objects? | |
| Nov 10, 2016 at 13:10 | comment | added | Dmitrii Bychenko | @PaulF: thank you! I see! I should have been more accurate when dealing with / commenting low level things. I've edited the answer. | |
| Nov 10, 2016 at 13:08 | history | edited | Dmitrii Bychenko | CC BY-SA 3.0 |
added 36 characters in body
|
| Nov 10, 2016 at 13:04 | comment | added | PaulF | @DmitryBychenko - if you disassemble the code generated then you will see that i.ToString() is not called, the int is boxed & then concat is called - if a string object is created it is within the .Net code & can be ignored just as the creation of the empty string can. Replacing s=s+i; with s=s+i.ToString(); does explicitly create a new string before calling concat. | |
| Nov 10, 2016 at 11:04 | comment | added | Lasse V. Karlsen |
Bear in mind that if you somehow trick something to manually construct a string which also happens to be empty, without using a string literal, you may end up with another empty string object in memory. The JITter or .NET is pretty "smart" though so this, for instance - string s = new string(' ', 0); - won't create another string object, it will still reference the empty string object that already exists. This may also be the result of the string constructor being external code that simply returns the empty string reference though so it may not be the JITter in this example.
|
|
| Nov 10, 2016 at 11:00 | vote | accept | Vivek Nuna | ||
| Nov 10, 2016 at 10:39 | comment | added | Vivek Nuna | @LasseV.Karlsen, It was very nice discussion with you and Dmitry Bychenko | |
| Nov 10, 2016 at 10:33 | comment | added | Dominik | Lasse V. Karlsen is right. Strings are cached. When you create a string which has the same content than another string that already exists you only have 1 object(2 references to that 1 object). This works because string is immutable which means you can not change it. Whenever you try to change it you create a new instance (or reference another if an already existing string equals another yours after change) | |
| Nov 10, 2016 at 10:23 | comment | added | Lasse V. Karlsen | String literals in code are interned by the JITter at JITting time. Yes, a string object is created to hold the empty string, but it isn't created by the code in the question. It was created way earlier when the program started running, by the runtime. Whether this code was present at all makes no difference, the empty string was still created. As such, since the question ask "by below code", then the empty string doesn't count. It wasn't created by that code. Instead, a reference to the existing empty string was used instead which did not create another string object in memory. | |
| Nov 10, 2016 at 10:23 | comment | added | Vivek Nuna | @DmitryBychenko Thanks for your answer. I have edited my question with example, because I dont think that "no object create when I do string str = "" " | |
| Nov 10, 2016 at 10:17 | comment | added | Dmitrii Bychenko |
@sr28: yes, and string.Empty fits this definition (contains zero symbols); since string is immutable class, you can share the instances, i.e. don't create them but take from cache
|
|
| Nov 10, 2016 at 10:15 | comment | added | sr28 | Not sure I understand. If I check MSDN it says "An empty string is an instance of a System.String object that contains zero characters". I read this to mean that this creates a new string instance. | |
| Nov 10, 2016 at 10:06 | history | edited | Dmitrii Bychenko | CC BY-SA 3.0 |
added 186 characters in body
|
| Nov 10, 2016 at 10:03 | comment | added | Dmitrii Bychenko |
@sr28: "" is cached as string.Empty; you can convince youself by if (Object.ReferenceEquals(s, string.Empty)) Console.Write("cached");
|
|
| Nov 10, 2016 at 10:02 | comment | added | sr28 |
In your first block of code surely string s = ""; would create a string object, just one that contains 0 characters?
|
|
| Nov 10, 2016 at 10:00 | comment | added | Lasse V. Karlsen |
Then the options are wrong. Additionally, i will be boxed for each iteration which will create an additional object (not a string though).
|
|
| Nov 10, 2016 at 10:00 | comment | added | Vivek Nuna | But I have not been given option 20 | |
| Nov 10, 2016 at 9:55 | history | answered | Dmitrii Bychenko | CC BY-SA 3.0 |