C# 这是什么意思??这里是什么意思
可能重复:C# 这是什么意思??这里是什么意思,c#,null-coalescing-operator,C#,Null Coalescing Operator,可能重复: 这里的?符号是什么意思 我说的对吗:使用id,但如果id为空,则使用字符串“ALFKI” 公共操作结果选择客户端(字符串id) { ViewData[“客户”]=GetCustomers(); ViewData[“Orders”]=GetOrdersForCustomer(id??“ALFKI”); ViewData[“id”]=“ALFKI”; 返回视图(); } [行动] 公共操作结果\u选择客户端\u订单(字符串customerID) { customerID=custome
这里的
?
符号是什么意思
我说的对吗:使用id
,但如果id
为空,则使用字符串“ALFKI”
公共操作结果选择客户端(字符串id)
{
ViewData[“客户”]=GetCustomers();
ViewData[“Orders”]=GetOrdersForCustomer(id??“ALFKI”);
ViewData[“id”]=“ALFKI”;
返回视图();
}
[行动]
公共操作结果\u选择客户端\u订单(字符串customerID)
{
customerID=customerID??“ALFKI”;
返回视图(新GridModel)
{
数据=GetOrdersForCustomer(customerID)
});
}
它的意思是“如果id
或customerID
为null
,则假装它是“ALFKI”
。它是null合并操作符:
当第一个值(左侧)为空时,它提供一个值(右侧)。这是
即:x
将被分配z
如果y
为null
,否则将被分配y
因此,在您的示例中,
customerID
将设置为“ALFKI“
如果它最初是null
,这是一个很好的解释方法。+1对于问题有一个合适的用户名,我的假设是正确的。感谢您的详细解释。当我的代码从代码审查过程中返回时,我通常会看到??
后面的字母WTF
:-)搜索了大约3种不同的变体,但没有找到任何结果。我猜是用错了词……问题是你不能在SO或Google上搜索?
。。。
public ActionResult SelectionClientSide(string id)
{
ViewData["Customers"] = GetCustomers();
ViewData["Orders"] = GetOrdersForCustomer(id ?? "ALFKI");
ViewData["id"] = "ALFKI";
return View();
}
[GridAction]
public ActionResult _SelectionClientSide_Orders(string customerID)
{
customerID = customerID ?? "ALFKI";
return View(new GridModel<Order>
{
Data = GetOrdersForCustomer(customerID)
});
}
var x = y ?? z;
// is equivalent to:
var x = (y == null) ? z : y;
// also equivalent to:
if (y == null)
{
x = z;
}
else
{
x = y;
}