gpt4 book ai didi

c# - C#/.NET 3.5 中的转换如何适用于具有 '?' 的类型

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:27 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Nullable types and the ternary operator. Why won’t this work?

这是我的有效代码

public decimal? Get()
{
var res = ...
return res.Count() > 0 ? res.First() : (decimal?) null;
}

这个不行

public decimal? Get()
{
var res = ...
return res.Count() > 0 ? res.First() : null;
}

给出编译器错误:

Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'decimal' and '<null>'

我想知道为什么?有什么想法吗?

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