Search code examples
c#.netmonoclrstring-interning

Does a call to ToString() on a boolean always allocate memory


Consider the following code:

private bool flag;

private void Test()
{
    Console.WriteLine(flag.ToString());
}

Suppose that Test() is called multiple times, will it allocate memory every time, or is there some mechanism in C# (compiler or runtime) that optimizes this ?

I've heard of "String interning", but i am not sure if it's performed for scenarios such as this one, or only when string constants are involved.


Solution

  • Nope, you can see for yourself in sources, the ToString() method is returning value of readonly static fields.