Why didn't the compiler use the same object reference for the static expression tree?

As I know, expression trees are immutable , so why didn't the compiler use the same object reference for a static expression as string literals?

To clarify the issue, see an example:

static void Main(string[] args)
{
    Test(p => true);//2637164
    Test(p => true);//3888474
    Test("true");//-292522067
    Test("true");//-292522067
    Console.ReadKey();
}

public static void Test(Expression<Func<string,bool>> exp)
{
    Console.WriteLine(exp.GetHashCode());
}

public static void Test(string str)
{
    Console.WriteLine(str.GetHashCode());
}
+4
source share
2 answers

As I know, expression trees are immutable, so why did the compiler not use the same object reference for a static expression, such as string literals?

The spectrum says that the compiler is allowed but not required to set identical lambdas.

String literals are interned at run time for free; There is no cost to the compiler developer.

, -, , , "" , , , Visual Studio . .

+8

, : IEqualityComparer . , , , Object.ReferenceEquals, , , , , .

0

Source: https://habr.com/ru/post/1689179/


All Articles