Search code examples
.netclr

Does the CLR intern string constants?


Lately I've been reading up on how the string intern pool works. However I haven't been able to find the answer to this question.

If I declare a constant string variable like const string STR = "foo";, does this also get added to the intern table?


Solution

  • You can find out:

    const string STR = "foo";
    
    string internedFoo = String.IsInterned("foo");    
    if (internedFoo != null)  // yes it is !
    

    The answer will be yes in any version of the framework you can find, but it is implementation dependent. And there exists an obscure setting to turn interning off.