这个问题已经在这里有了答案:
已关闭10年。
我试图了解此语句的作用:“??”是什么意思吝啬的?
if语句是som类型吗?
string cookieKey = "SearchDisplayType" + key ?? "";
最佳答案
它是Null Coalescing运算符。这意味着如果第一部分具有值,那么将返回该值,否则它将返回第二部分。
例如。:
object foo = null;
object rar = "Hello";
object something = foo ?? rar;
something == "Hello"; // true
或一些实际的代码:
IEnumerable<Customer> customers = GetCustomers();
IList<Customer> customerList = customers as IList<Customer> ??
customers.ToList();
此示例正在做的工作是将客户转换为
IList<Customer>
。如果此强制转换结果为null,则它将在客户IEnumerable上调用LINQ ToList
方法。可比的if语句是这样的:
IEnumerable<Customer> customers = GetCustomers();
IList<Customer> customersList = customers as IList<Customer>;
if (customersList == null)
{
customersList = customers.ToList();
}
与使用null-coalescing运算符在一行中完成代码相比,这是很多代码。
关于c# - '??'在C#中是什么意思?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3767751/