会话像原始类型一样传递?

时间:2018-05-20 04:54:58

标签: c# .net asp.net-mvc pass-by-reference pass-by-value

我有一个MVC Core Dotnet应用程序,基本上我想加载会话数据视图,但也在页面加载后删除会话。我的代码有效,但我不明白为什么

public IActionResult Charge(string stripeEmail, string stripeToken, int totalPrice){
    //some stripe code that went here...

    var cart = HttpContext.Session.GetCart();
    ClearSession();
    return View(cart);
}

protected void ClearSession(){
    //creates a new instance if cart doesnt exist otherwise returns it
    var cart = HttpContext.Session.GetCart();

    //clears the items from the collection using .Clear() ...
    cart.RemoveAll();
    HttpContext.Session.SetCart(cart);
}

当代码放在一个像这样的新方法时,代码就可以工作..但是如果我把它放在同一个方法中它就不起作用了。我很惊讶将它放在一个新的方法中,因为它违背了我对如何通过值传递对象类型的理解。(对象的位置将被传递)。当我清除新方法中的项目列表时,它就像对象的副本一样。

为什么这段代码有效?

0 个答案:

没有答案