gpt4 book ai didi

c# - 类型名称后的符号 '?' 是什么意思

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

我正在使用 Eto gui framework .我在他们的源代码中看到了一些神奇的语法;例如:

int x;
int? x;
void func(int param);
void func(int? param);

有什么不同?我很迷惑。符号 ? 很难用谷歌搜索。

最佳答案

这意味着它们是Nullable , 它们可以保存空值

如果你定义了:

int x;

那么你不能这样做:

x = null; // this will be an error. 

但是如果你定义了x作为:

int? x;

然后你可以这样做:

x = null; 

Nullable<T> Structure

In C# and Visual Basic, you mark a value type as nullable by using the ? notation after the value type. For example, int? in C# or Integer? in Visual Basic declares an integer value type that can be assigned null.

我个人会使用 http://www.SymbolHound.com要使用符号搜索,请查看结果 here

?只是语法糖,相当于:

int? xNullable<int> x 相同

关于c# - 类型名称后的符号 '?' 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15082221/

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