2017-07-17 82 views
0

我有一個類如下:如何清除包含C#中的列表的會話變量?

public class Employee 
{ 
    public int EmployeeID { get; set; } 
    public string Name { get; set; } 

    public List<int> ProjectIDs { get; set; } 
} 

,並在一個點上我存儲在清單會話變量,就像這樣:

List<Employee> employees = new List<Employee>(); 
// populate employees 

Session["EmployeeList"] = employees; 

當我用它做的,我想清楚了它,所以我做的:

Session["EmployeeList"] = null; 

僱員和名稱是清除出去,而不是List<int> ProjectIDs這就是它。如何確保Session["EmployeeList"]完全被清除?

+2

你不需要。垃圾收集器知道如何完成它的工作。 – SLaks

+0

@SLaks在我的問題中修正了一個拼寫錯誤。我的意思是指出,當我測試它時,session變量中的'List '沒有被清除,而'EmployeeID'和'Name'都被清除了。 – Darren

+0

你是說你給'Session [「EmployeeList」]'分配了'null',然後你仍然可以從'Session [「EmployeeList」]''中檢索一個有效的列表?這聽起來不對。 –

回答

0

您可以使用Remove()方法上Session對象說

Session.Remove("EmployeeList"); 

順便說一句,你說什麼不應該的;一旦你設置會話對象爲空說Session["EmployeeList"] = null; ...然後試圖找回與該密鑰相同的會話對象((List<Employee>)Session["EmployeeList"]) == null ;應該返回空

+0

你是對的,事實證明,代碼將它設置回其他地方,它被保存到會話中。謝謝! – Darren