什么是“?”和“?”意思是这个c#代码。 [英] What does "?" and "?" mean in this c# code.
本文介绍了什么是“?”和“?”意思是这个c#代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是?和?在这个c#代码中表示。
what does "?" and "?" mean in this c# code.
object obj = lstbxCats.SelectedItem;
string selected = ( obj == null )
? "No item selected."
: (string) ( (ListBoxItem) obj ).Content;
我是什么尝试过:
i正在读一本关于wpf c#编程的书,我遇到了这段代码
What I have tried:
i was reading a book on wpf c# programming and i came across this code
推荐答案
这是三元运营商 [ ^ ]。基本上,它是一个if / then / else表达式。
代码正在评估(obj == null)
。如果为null(结果为true),则表示未选择任何项目。返回字符串。如果没有,则返回obj
的内容(强制转换为ListBoxItem),转换为字符串。
It's the Ternary operator[^]. Basically, it's a if/then/else expression.
The code is evaluating(obj == null)
. If it is null (result is true), the "No item selected." string is returned. If not, the Content of theobj
(cast to a ListBoxItem) is returned, cast as a string.
这篇关于什么是“?”和“?”意思是这个c#代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文