gpt4 book ai didi

c# - 隐式和显式访问可空类型的值

转载 作者:行者123 更新时间:2023-11-30 14:31:11 25 4
gpt4 key购买 nike

Nullable<int> 时有什么不同吗?是隐式访问还是显式访问?

    Nullable<int> x = 10;
Console.WriteLine("Value: {0}", x.Value);
Console.WriteLine("Value: {0}", (int)x);

两者都打印 10,但我想知道是否有任何特定偏好使用一个而不是另一个。

最佳答案

显式转换运算符定义如下:

public static explicit operator T(T? value)
{
return value.Value;
}

所以实际上,根本没有区别,因为它无论如何都使用 Value 属性。

它在 MSDN 上也有很好的描述:Nullable Explicit Conversion (Nullable to T)

Return Value
Type: T

The value of the Value property for the value parameter.

关于c# - 隐式和显式访问可空类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20907248/

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