Given the following simple .NET code, is there any difference between these two, under the hood with respect to the string \"xml\"
?
if (extension.Eq
There is no difference due to the fact that literal strings are normally interned by the compiler.
All the following examples will print out true
:
Example 1:
const string constHello = "Hello";
void Foo()
{
var localHello = "Hello";
Console.WriteLine(ReferenceEquals(constHello, localHello));
}
Example 2:
string fieldHello = "Hello";
void Foo()
{
var localHello= "Hello";
Console.WriteLine(ReferenceEquals(fieldHello, localHello));
}
Example 3:
const string constHello = "Hello";
void Foo()
{
Console.WriteLine({IsReferenceEquals("Hello")}");
}
public static bool IsReferenceEquals(string s) => ReferenceEquals(constHello, s);
In all cases, there is only one instance of "Hello"
.