这是什么作用??这里的符号意思是

发布于 2024-09-26 06:44:35 字数 852 浏览 1 评论 0原文

可能的重复:
什么是“??”运算符?

这里的 ?? 符号是什么意思?

我说得对吗:使用id,但如果id为空,则使用字符串“ALFKI”?

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)
            });
        }

Possible Duplicate:
What is the “??” operator for?

What does the ?? notation mean here?

Am I right in saying: Use id, but if id is null use string "ALFKI" ?

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)
            });
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇到 2024-10-03 06:44:35

这是 空合并运算符。

var x = y ?? z;

// is equivalent to:
var x = (y == null) ? z : y;

// also equivalent to:
if (y == null) 
{
    x = z;
}
else
{
    x = y;
}

即:xynull,code>将被分配z,否则将被分配y
因此,在您的示例中,如果 customerID 最初为 null,则它将设置为 "ALFKI"

That's the null-coalescing operator.

var x = y ?? z;

// is equivalent to:
var x = (y == null) ? z : y;

// also equivalent to:
if (y == null) 
{
    x = z;
}
else
{
    x = y;
}

ie: x will be assigned z if y is null, otherwise it will be assigned y.
So in your example, customerID will be set to "ALFKI" if it was originally null.

马蹄踏│碎落叶 2024-10-03 06:44:35

这是空合并运算符:
http://msdn.microsoft.com/en-us/ library/ms173224(VS.80).aspx

当第一个值(左侧)为空时,它提供一个值(右侧)。

It's the null coalescing operator:
http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx

It provides a value (right side) when the first value (left side) is null.

我要还你自由 2024-10-03 06:44:35

它的意思是“如果 idcustomerIDnull,则假装它是 "ALFKI"

It means "if id or customerID is null, pretend it's "ALFKI" instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文