In the MSDN, I found this sentence:
The result of a ?? operator is not considered to be a constant even if both its arguments are constants.
What does this signify? Do they mean that the compiler optimiser does not know that this value is constant? I don't see another way this could be relevant.
Edit: And why is this not considered constant? Is there a logical reason for it?
Edit: This piece of code gives a compiler error for x, but not for y, why can ?:
result in a constant value, but ??
cannot?
const string NULL = null;
const string val = "value";
const string x = NULL ?? val;
const string y = NULL == null ? val : NULL;
Let's think about usage of ?? operator in constant expressions. Is it possible in principle? Of course, because we have all required information at compile time. Is it implemented in C#? Nope. Why? Because C# compiler team chose not to implement this feature. Here is what Eric Lippert says about selecting features which should be implemented:
Features have to be so compelling that they are worth the enormous dollar costs of designing, implementing, testing, documenting and shipping the feature. They have to be worth the cost of complicating the language and making it more difficult to design other features in the future.
When we can use ?? operator in constant expression? When expr-first is a null literal. And constant type is either object or string (remember, C# team already decided not to add Nullables support for constants). And what this feature will add to the language?
const string s = null ?? "foo";
That does not add any convenience to the language. It's totally same as
const string s = "foo";
but more complicated.