lysergic-acid
lysergic-acid

Reputation: 20050

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.

Upvotes: 0

Views: 479

Answers (1)

Sinatr
Sinatr

Reputation: 21999

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

Upvotes: 2

Related Questions