سؤال

تكرار ممكن:
ما هو "؟؟" مشغل؟

ماذا يكون ال ?? تدوين يعني هنا؟

هل أنا محق في القول: استخدم 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)
            });
        }
هل كانت مفيدة؟

المحلول

هذا هو مشغل coalescing فارغ.

var x = y ?? z;

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

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

بمعنى آخر: x وسيخصص z إذا y هو null, ، وإلا سيتم تعيينه y.
لذلك في مثالك ، customerID سيتم ضبطه على "ALFKI" إذا كان في الأصل null.

نصائح أخرى

إنه المشغل الفارغ:http://msdn.microsoft.com/en-us/library/ms173224(vs.80).aspx

يوفر قيمة (الجانب الأيمن) عندما تكون القيمة الأولى (الجانب الأيسر) فارغة.

وهذا يعني "إذا id أو customerID هو null, ، يتظاهر بذلك "ALFKI" في حين أن.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top