C# ASP.NET 从会话中删除项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/261920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET removing an item from Session?
提问by David Basarab
Which method is preferred?
哪种方法是首选?
Session.Remove("foo");
Session["foo"] = null;
Is there a difference?
有区别吗?
采纳答案by Buu Nguyen
Is there a difference?
有区别吗?
There is.
Session.Remove(key)
deletes the entry (both key & value) from the dictionary while Session[key] = null
assigns a value (which happens to be null) to a key. After the former call, the key won't appear in the Session#Keys
collection. But after the latter, the key can still be found in the key collection.
有。
Session.Remove(key)
从字典中删除条目(键和值),同时Session[key] = null
为键分配一个值(恰好为空)。前一次调用后,该键将不会出现在Session#Keys
集合中。但是在后者之后,仍然可以在密钥集合中找到密钥。
回答by dove
I would go with Remove but can not honestly say if there is a difference. At a guess there may still be an empty key kept for that null value but not sure. Remove would give me little doubt and if that's what you want to do it reads betterin code as well.
我会选择 Remove 但不能老实说是否有区别。猜测可能仍然为该空值保留了一个空键,但不确定。Remove 会给我一点疑问,如果这就是你想要做的,它在代码中也能更好地阅读。
回答by splattne
It has the same effect. I personally think that the Session.Remove
method does express the programmer's intent better.
它具有相同的效果。我个人认为该Session.Remove
方法确实更好地表达了程序员的意图。
Here some links to the documentation on MSDN:
这里有一些指向 MSDN 文档的链接:
"HttpSessionState.Item Property:
Property Value
Type: System.Object
The session-state value with the specified name, or null reference (Nothing in Visual Basic) if the item does not exist."
“HttpSessionState.Item 属性:
属性值类型:System.Object
具有指定名称的会话状态值,如果该项不存在,则为空引用(在 Visual Basic 中为 Nothing)。”
回答by FlySwat
The biggest difference is how you read from session.
最大的区别是你从 session 中读取的方式。
if(Session.ContainsKey["foo"]) { return Session["foo"]; }
or
或者
if(Session["foo"] != null) { return Session["foo"]; }
If you use the first method, setting the value to null will not work, and you should use remove.
如果您使用第一种方法,将值设置为 null 将不起作用,您应该使用 remove。
If you use the second method, you can set the value to null.
如果使用第二种方法,则可以将该值设置为 null。
回答by JakubRi
I know this is old thread but definitely stick with Session["key"] = null
- it's much more faster! I've done some tests (on InProc Session State), removing 1000 items in row (elapsed time is for 1000 items totally, so if you want average time for one item, just divide it with 1000):
我知道这是旧线程,但绝对坚持Session["key"] = null
- 它更快!我做了一些测试(在 InProc 会话状态上),连续删除 1000 个项目(经过的时间总共是 1000 个项目,所以如果你想要一个项目的平均时间,只需将它除以 1000):
Removing 1000 existing items:
删除 1000 个现有项目:
Session[key] = null; - 0.82380000000000009 ms
Session.Remove(key); - 59.960100000000004 ms
Removing 1000 NOT existing items:
删除 1000 个不存在的项目:
Session[key] = null; - 1.5368000000000002 ms
Session.Remove(key); - 0.6621 ms
Removing 500 existing and 500 not existing items:
删除 500 个现有和 500 个不存在的项目:
Session[key] = null; - 1.0432000000000001 ms
Session.Remove(key); - 33.9502 ms
Here is a piece of code for first test:
这是第一次测试的一段代码:
Session.Clear();
for (int i = 0; i < 1000; i++)
Session[i.ToString()] = new object();
Stopwatch sw1 = Stopwatch.StartNew();
for (int i = 0; i < 1000; i++)
Session[i.ToString()] = null;
sw1.Stop();
Session.Clear();
for (int i = 0; i < 1000; i++)
Session[i.ToString()] = new object();
Stopwatch sw2 = Stopwatch.StartNew();
for (int i = 0; i < 1000; i++)
Session.Remove(i.ToString());
sw2.Stop();