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.
Nope, you can see for yourself in sources, the ToString()
method is returning value of readonly static
fields.