您的位置:首页 > 其它

Session.Clear()与Session.RemoveAll()以及Session.Abandon()有什么区别?

2009-07-30 17:07 555 查看
首先给出微软在HttpSessionState类中两个方法的实现:
public void Clear()
{
_dict.Clear();
}
public void RemoveAll()
{
Clear();
}

RemoveAll 方法调用了 Clear 方法。
msdn对三个函数的描述是:
HttpSessionState.Clear :Removes all keys and values from the session-state collection.
HttpSessionState.RemoveAll:Removes all keys and values from the session-state collection.
HttpSessionState.Abandon :Cancels the current session.

以下摘自 论坛:http://forums.asp.net/
Both do the same thing - remove all entries from the session state collection.
There is no difference. RemoveAll just calls Clear. So it is better to call Clear instead of RemoveAll.

另一个论坛:
Is there a difference between Session.Clear() and Session.RemoveAll()? The
descriptions and documentation pages seem to say exactly the same thing, but
I am assuming there must be some reason for creating two functions, right?
Thanks.
--
Nathan Sokalski

You are correc that they do the same exact thing. And that is because
..RemoveAll calls .Clear. So, why do they both exist? Got me! Perhaps it
was just a lack of communication when the MS employees were developing
asp.net.

Ray at home

又一个论坛:
What is the best way to end session?
Hi all,

I noticed that there are three methods that can remove session
variables
Session.Clear()
Session.RemoveAll()
Session.Abandon()

Now my question as is stated on the subject, what is the best way to
end a session?
I know that the name itself is telling me that Session.Abandon() should
be the methond called when ending a session, but if the other two
methods clear or remove all the variables, isn't that the same, since
if I want to go to a page that requires a session variable it just
won't have it?
Actually I found using either RemoveAll or Clear, better than Abandon,
since the Remarks on the Abandon method on MSDN already state that you
can still access session variables after calling the method.
Could anybody explain the difference among these methods?

Re: What is the best way to end session?
The first 2 only remove session variables you have stored so far. The
session itself is still alive.

Session.Abandon is the only one of those that actually kills the session.

"Hugo Flores" <hugo.flores [at] ge.com> wrote in message
http://www.cnblogs.com/moneyriver2006/admin/news:1121360161.953896.131270 [at] g47g2000cwa.googlegroups.com.. .

Marina [ Do, 14 Juli 2005 19:09 ] [ ID #878685 ]
Re: What is the best way to end session?
Clear() and RemoveAll() perform the same thing: remove the session variables
but keep the current session in memory. Whereas, Abandon() ends the current
session.

"Hugo Flores" <hugo.flores [at] ge.com> wrote in message
http://www.cnblogs.com/moneyriver2006/admin/news:1121360161.953896.131270 [at] g47g2000cwa.googlegroups.com.. .

因此,在退出登录时我的一般写法是:
Session.Clear();
Session.Abandon();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: