Curioddity Number 4: how many strings do I have? [Kit George]

Well its time for another curioddity. This one is pretty fun, there's a couple of good sidesteps involved. The following code is gong to write out 3 lines of code to the Console. Your tasks are to:

1: without running the code, write down what you think will be written out

2: run the code, and see for yourself what was written out

3: figure out, if 1 and 2 don't match, what the heck is going on!

using System.Text;

using System;

class intern


    static unsafe void ModifyConst() {

        string str1 = "Hello";

        fixed(char* pstr = str1) {

            pstr[0] = 'X';



    static void Main() {


        StringBuilder sb = new StringBuilder("Hel");


        string str = sb.ToString();



        switch(str) {

            case "Xello":

               Console.WriteLine("string is Xello"); break;

            case "Hello":

                Console.WriteLine("string is Hello"); break;


                Console.WriteLine("Not Found"); break;




Comments (8)

  1. Anonymous says:



    Not Found

    Hello is first. The SB will put this string together without reference to the interned string.

    Xello second. The ModifyConst method changes the interned string. any literal instances of Hello are changed.

    Not Found Next. As per above, the case "Hello" which is interned will be changed by modifyconst to "Xello". As such, there is no match to the "Hello" string which is created by the SB.

    The name of the class was too good of a hint.

  2. Anonymous says:

    Using an Unsafe code block, ModifyConst() modifies the Const in memory to "Xello".

    The compiler is pretty smart, knowing that strings are immutable in .NET, reuses the memory for the literals defined in Main().

    The literal "Hello" in the following two lines of code from Main() are actually evaluated to "Xello". This can be seen in the IL.



    print "Xello"




    case "Hello":

    is the same as comparing

    str = Modified Literal

    "Hello" = "Xello"

  3. Anonymous says:

    I’ve already learn that from

    They’re so similar. And I wounder where this info originally came from? Could I learn more from there?


  4. Anonymous says:

    Sumtec, I put this code in my blog to demostrate the string interning and CLR’s handling of switch/case. It doesn’t come from any book or anything. Jeffrey Richter’s "Applied Microsoft .NET Framework Programming" is the best reference book I can find on this topic.

  5. Anonymous says:

    Apologies if this originated elsewhere, and kudos to the originator, it’s an excellent brain-twister.

  6. Anonymous says:

    Hi, Kit. There is no problem. I’m actually glad to see that there are people reading my blogs. 🙂

  7. Anonymous says:

    Maybe it is my fault.

    I orginally saw the materials in and told somebody in microsoft.

    And the answer is in switch case, str = "Hello" both compares with "Xello"

Skip to main content