问号在C#代码中是什么意思? [英] What does a question mark mean in C# code?

查看:77
本文介绍了问号在C#代码中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了类似以下不相关行的代码:

I've seen code like the following unrelated lines:

 Console.Write(myObject?.ToString());
 return isTrue ? "Valid" : "Lie";
 return myObject ?? yourObject;
 int? universalAnswer = 42;

问号的所有用法是否相关或不同?他们每个人是什么意思?

Are all of the usages of the question mark related or different? What do each of them mean?

推荐答案

根据上下文,问号在C#中具有不同的含义.

Question marks have different meaning in C# depending on the context.

空条件运算符( MSDN 成员中的问号是什么C#中的访问均值?)

Console.Write(myObject?.Items?[0].ToString());

条件运算符/三元运算符( MSDN 使用条件?:(三元)运算符的好处)

return isTrue ? "Valid" : "Lie";

空合并运算符( MSDN 两个问号在C#中在一起意味着什么?)

return myObject ?? yourObject;

可为空的类型( MSDN 是什么类型后面问号的目的(例如:int?myVariable)?)

int? universalAnswer = 42;

这篇关于问号在C#代码中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆