1

Its better known that strings are immutable => meaning, that the contents of the object cannot be changed after it is created.

So, the interview question was:

How many objects were created in this statement?

string q = "A" + "B" + "C";

I answered two because "A" + "B" creates one object and concatenating with "C" creates an another one.

But, she said its wrong. Any ideas why ?

And what happens when concatenation of multiple strings take place like in this scenario ?

  • 7
    Were you interviewing to write a compiler or something similar? Because if not, that's a dumb interview question. – Katie Kilian Nov 20 '13 at 15:23
  • 1
    Since these are compile-time statements, I'd expect "one" - `string q = "ABC";` – Dan Pichelman Nov 20 '13 at 15:24
  • 1
    if it's as shown then the answer is probably none - since all of the constant strings should be concatenated at compile time# – Damien_The_Unbeliever Nov 20 '13 at 15:24
  • @CharlieKilian: I know that the interview question is theoritical. but still, this bothers me. – now he who must not be named. Nov 20 '13 at 15:24
  • The C# compiler will probably convert it to a `String.Concat` call, or just create a single string at compile, anyway (for performance), so it'll create one object. – vcsjones Nov 20 '13 at 15:24
  • @Damien_The_Unbeliever: Wouldn't it be one (the one in `q`)? – Ken White Nov 20 '13 at 15:26
  • @Damien_The_Unbeliever: how do you conclude that? by use of reflector or something ? – now he who must not be named. Nov 20 '13 at 15:26
  • 4
    See [String concatenation behind the scenes, part one](http://ericlippert.com/2013/06/17/string-concatenation-behind-the-scenes-part-one/) by Eric Lippert. It would evaluate to a compile time literal since there are no variables. – Dustin Kingen Nov 20 '13 at 15:27
  • @nowhewhomustnotbenamed. It's pretty common knowledge really... – Servy Nov 20 '13 at 15:27
  • 1
    @KenWhite - no, in that *statement*, there's a constant string that already exists and the variable `q` is made to reference that string. *no* new objects are created by that statement – Damien_The_Unbeliever Nov 20 '13 at 15:27
  • 1
    Oh, interview questions are interesting. Usually it's about finding out what's going on in your mind, so you should have explained how you find the answer, not what the answer is. In my mind, it depends on Debug mode or Release mode. In Release mode the compiler could optimize everything away, leaving just q. Another possible answer is "A", "B", "AB", "C" and "ABC", which is 5. I guess, if you explained it like this, you have good chances to get the job. The interviewer has then found out how you are analyzing a given situation. I'm interested in the answers though... – Thomas Weller Nov 20 '13 at 15:28
  • It will create one object as is answered over here: [link][1] [1]: http://stackoverflow.com/questions/9132338/how-many-string-objects-will-be-created-when-using-a-plus-sign – liquidsnake786 Nov 20 '13 at 15:28
  • @Damien: Thanks. Didn't look at it that way. Was looking at that var probably being used later, rather than as a constant. (As was mentioned in the post liquidsname786 linked.) – Ken White Nov 20 '13 at 15:28
  • @KenWhite - since the string is a literal that's concated by the compiler, wouldn't it be interned? So it would exist prior to the assignment, and the assignment itself creates no objects. – hatchet - done with SOverflow Nov 20 '13 at 15:29
  • 1
    @nowhewhomustnotbenamed It would bug me too. But it also bugs me when the interviewers as a question that tells you nothing about how well the candidate is going to perform at their job. Also, that they would ask that question tells YOU something about their work environment. – Katie Kilian Nov 20 '13 at 15:29
  • 1
    @Damien_The_Unbeliever No, assuming that something actually uses the string later on (otherwise it's optimized out altogether) then a `ldstr` instruction will be issued, which creates one object. As the documentation for `ldstr` says: *The ldstr instruction pushes an object reference (type O) to a new string object representing the specific string literal stored in the metadata. The ldstr instruction allocates the requisite amount of memory and performs any format conversion required to convert the string literal from the form used in the file to the string format required at runtime.* – Matthew Watson Nov 20 '13 at 15:41

2 Answers2

15

string q = "A" + "B" + "C"; will be converted by the compiler into:

string q = "ABC";

because all the strings are constants. So the correct answer is that only one object is created.

Technically, it gets compiled to the following IL:

ldstr "ABC"

Note that if the strings weren't constant (i.e. you used variables there), it would be converted to a single call to String.Concat(string, string, string)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Great answer though. But what if, `var q = string1 + string2 + string3` ? Does it be converted to a string.concat(string1, string2, string3) => the answer is one ? – now he who must not be named. Nov 20 '13 at 15:31
  • There would be 4 strings created. One for each literal and one for the concatenation. Plus whatever objects are created in `string.Concat`. – Dustin Kingen Nov 20 '13 at 15:34
  • What about the effect of interning of string literals? Wouldn't the object already exist prior to the assignment because of that? – hatchet - done with SOverflow Nov 20 '13 at 15:34
  • @hachet It's loading a string reference into a variable. A reference is still an object. – Dustin Kingen Nov 20 '13 at 15:35
  • 1
    @nowhewhomustnotbenamed. Well in that case we would already have 3 separate string objectss, but only one new string object would be created. – Matthew Watson Nov 20 '13 at 15:36
  • But the question is "how many objects were created in this statement". The object was created prior to the statement, so the answer would be zero. It becomes more clear if there where a similar statement following it that was identical, except it assigned to `string r`. http://stackoverflow.com/questions/8692303/intern-string-literals-misunderstanding – hatchet - done with SOverflow Nov 20 '13 at 15:36
  • 1
    @hatchet The [documentation for `ldstr`](http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.ldstr%28v=vs.110%29.aspx) states: *Pushes a new object reference to a string literal stored in the metadata*, so a new object will be created. – Matthew Watson Nov 20 '13 at 15:37
  • You say: "Technically, it gets compiled to the following IL". It depends on the code before and after the statement what the compiler does. The ldstr may even be optimized away. – Thomas Weller Nov 20 '13 at 15:46
  • @ThomasW. I'm assuming that the string is used afterwards. Without other information, that seems to be the logical assumption to make in the context of that interview question. Note that if the string is used in any way it will result in `ldstr "ABC"` being generated. – Matthew Watson Nov 20 '13 at 15:50
1

Zero if this is the only statement in the Main() method. Just write the following code and use Reflector:

namespace HowManyStrings
{
    class Program
    {
        static void Main(string[] args)
        {
            string q = "A" + "B" + "C";
        }
    }
}

37 if you actually run the program with the empty Main() method:

0:000> !dumpheap -stat -type String
Statistics:
      MT    Count    TotalSize Class Name
6876ab98        2          104 System.Object[]
687bacc0       37         2078 System.String
Total 39 objects

(in a 32 Bit .NET 4.0.30319.18052 Console application on Windows 7)

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • While technically correct, I think this probably misses the underlying point of the question. – Matthew Watson Nov 20 '13 at 15:39
  • What's your basis for assuming that `q` is never used? The question never states that this is the entirety of the program. – Servy Nov 20 '13 at 15:40
  • @MatthewWatson, Servy: exactly this is the point: we make assumptions about Release build, Debug build and internal implementations of the .NET framework. Who knows, maybe .NET creates an intermediate String while concatenating, so it's always one more than we figure out here. – Thomas Weller Nov 20 '13 at 15:44
  • @ThomasW. I really don't think that this would be the answer that the interviewer would be looking for. But without being able to ask them, we'll never know. :) – Matthew Watson Nov 20 '13 at 15:51