Given the following simple .NET code, is there any difference between these two, under the hood with respect to the string "xml"
?
if (extension.Equals("xml", StringComparison.OrdinalIgnoreCase))
{
return FileType.Xml;
}
vs
const string xmlText = "xml";
if (extension.Equals(xmlText, StringComparison.OrdinalIgnoreCase))
{
return FileType.Xml;
}
Mind you, the word "xml"
is never used again in the class. It's literally at that one spot.
I was under the impression that the more recent versions of the .NET compiler converts simple strings like that, to a const (under the hood).
Even more awesome, is that if there's another "xml"
string used in another place, the compiler will only make one const creation and both of those references will refer to that single string reference/memory alloc.
So - is there any difference, behind the scenes with respect to performance?
Disclaimer: please no 'micro-optimation' comments. I get that. This is really about understanding things vs trying to micro-optimize one line of code.