gpt4 book ai didi

c# - "The result is not considered to be constant"是什么意思

转载 作者:太空狗 更新时间:2023-10-30 00:49:00 26 4
gpt4 key购买 nike

MSDN ,我找到了这句话:

The result of a ?? operator is not considered to be a constant even if both its arguments are constants.

这是什么意思?他们的意思是编译器优化器不知道这个值是常量吗?我看不到这可能相关的其他方式。

编辑: 为什么这不被视为常量?是否有合乎逻辑的原因?

编辑:这段代码给出了 x 的编译器错误,但 y 却没有,为什么 ?: 可以得到一个常量值,而 ? ? 不能?

const string NULL = null;
const string val = "value";
const string x = NULL ?? val;
const string y = NULL == null ? val : NULL;

最佳答案

这意味着您不能在常量中使用空合并运算符:

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

关于c# - "The result is not considered to be constant"是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com