gpt4 book ai didi

c# - bool 值有什么区别? MyProperty 和 bool ?MyProperty

转载 作者:太空狗 更新时间:2023-10-29 21:42:58 25 4
gpt4 key购买 nike

今天在审查 PR 时,我在类里面看到了这个:

public bool ?Selected { get; set; }   //question mark in front of property name

我怀疑是开发者故意的

public bool? Selected { get; set; }  //question mark at end of type

令我惊讶的是,这甚至符合要求,所以我自己尝试了一下,发现这是合法的代码,而且它最终做了同样的事情。

我的问题是:这两者之间是否有任何细微(或不太细微)的区别?

最佳答案

首先,我的 Visual Studio 2017 立即纠正了这一点

public bool ?Selected { get; set; } 

为此

public bool? Selected { get; set; } 

然后,IL DASM救援!可以看到得到的IL是一样的,就像@mjwills在评论里说的:

enter image description here

enter image description here

最后,你总是可以按 Ctrl+K + Ctrl+D 让 Visual Studio 重新格式化您的代码并正确管理空格和缩进。

根据您关于将 bool? 更改为 bool 的问题,这取决于:是否有一个 indefinite value or state , 就像 checkboxes , 你应该使用 bool?;否则 bool 没问题。

关于c# - bool 值有什么区别? MyProperty 和 bool ?MyProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49393491/

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