Which means "The result is not considered permanent."

On MSDN, I found this sentence:

Result The operator is not considered a constant, even if both arguments are constants.

What does it mean? Do they mean that the compiler optimizer does not know that this value is constant? I do not see another way that this could be relevant.

Edit: And why is this not considered permanent? Is there a logical reason for this?

Edit: this piece of code gives a compiler error for x, but not for y, why ?:can it lead to a constant value, but ??can't?

const string NULL = null;
const string val = "value";
const string x = NULL ?? val;
const string y = NULL == null ? val : NULL;
+4
source share
3 answers

?? . ? , . #? . ? # . , :

, , , , . .

? ? expr-first . - , (, # Nullables ). ?

const string s = null ?? "foo";

. ,

const string s = "foo";

.

+1

, :

public const int MyInt = 42; // Fine
public const int MyOtherInt = new int?(42) ?? 8; // Compiler error

, :

public const int SomeResult = 12 + 42; // Fine
public const int OtherResult = SomeResult * 2; // Fine

"". ?? :

var someValue = new int?(42) ?? 8; // Produces ldc.i4.8
+4

, , .

public void Test()
{
    const int x = ((int?)null ?? 3);
}

:

9 , "x",

, ?? .

In contrast, many other operators, such as ? :, are evaluated at compile time, so the following message does not generate an error:

const int x = (true ? 3 : 2);
+1
source

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


All Articles