gpt4 book ai didi

c# - 有没有办法在不进行 "if"检查的情况下获取空值?

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

我的代码目前看起来像这样:

if (App.cardSetWithWordCount == null)
App.cardSetWithWordCount = App.DB.GetCardSetWithWordCount(Settings.cc.Text());

我意识到这几乎只是一个单行检查,但想知道最新版本的 C# 现在是否有一种方法可以对这个结构进行编码,甚至更干净?

最佳答案

没有更简洁的方法,只有更冗长和可读性更差的方法,我认为您应该对现有的感到满意。

if (App.cardSetWithWordCount == null)
App.cardSetWithWordCount = App.DB.GetCardSetWithWordCount(Settings.cc.Text());

虽然你可以这样做

App.cardSetWithWordCount = App.cardSetWithWordCount ?? App.DB.GetCardSetWithWordCount(Settings.cc.Text());

其他资源

?? Operator (C# Reference)

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

关于c# - 有没有办法在不进行 "if"检查的情况下获取空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386949/

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