Redian新闻
>
A Question about Session State in ASP.NET
avatar
A Question about Session State in ASP.NET# DotNet - 窗口里的风景
d*t
1
Hi, All,
I have been confused by a seemingly simple question for a few days. Hope
anyone could give me some suggestion.
What bothers me is that ASP.NET seems to be able to remember the state of an
object after I put that object into the Session property. As you can find
in my code, I didn't explicitly save the object back to the Session property
after I changed it. But the state of the object seems to be updated in
following requests. Did I miss anything here? Thanks in advance.
Below is my code
avatar
l*s
2
This is how it suppose to work.
You are storing the arraylist object in the session and it's going to be the
'same' object whenever you retrieve it afterwards. And this arraylist
object has the references to the string items.

an
property

【在 d*****t 的大作中提到】
: Hi, All,
: I have been confused by a seemingly simple question for a few days. Hope
: anyone could give me some suggestion.
: What bothers me is that ASP.NET seems to be able to remember the state of an
: object after I put that object into the Session property. As you can find
: in my code, I didn't explicitly save the object back to the Session property
: after I changed it. But the state of the object seems to be updated in
: following requests. Did I miss anything here? Thanks in advance.
: Below is my code

avatar
d*t
3
Thanks for your answer. So it seems that what Session has is a reference to
the original object (not a clone of the object), which explains why no
explicit saving back of the object is necessary. I did try to manipulate
variables of primitive data types (such as int) in Session in the same way
but I had to save the variable back to Session every time after I changed it
. It is strange that none of the books I went through mentioned this matter,
neither does MSDN (or maybe I just didn't find it).

【在 l*s 的大作中提到】
: This is how it suppose to work.
: You are storing the arraylist object in the session and it's going to be the
: 'same' object whenever you retrieve it afterwards. And this arraylist
: object has the references to the string items.
:
: an
: property

avatar
l*s
4
This is not specific to session object. You need to read about value type/
reference type;boxing/unboxing;pass by value/pass by reference

to
it
matter,

【在 d*****t 的大作中提到】
: Thanks for your answer. So it seems that what Session has is a reference to
: the original object (not a clone of the object), which explains why no
: explicit saving back of the object is necessary. I did try to manipulate
: variables of primitive data types (such as int) in Session in the same way
: but I had to save the variable back to Session every time after I changed it
: . It is strange that none of the books I went through mentioned this matter,
: neither does MSDN (or maybe I just didn't find it).

avatar
d*t
5
My initial impression was that when objects were stored in a Session, their
clones were placed in a domain (such as SQL Server) separate from the
application domain. This is why I posted my original question. But now it
seems that only references are saved in session, even if an out-of-process
mode is used in Session. A

【在 l*s 的大作中提到】
: This is not specific to session object. You need to read about value type/
: reference type;boxing/unboxing;pass by value/pass by reference
:
: to
: it
: matter,

avatar
s*g
6
Not sure what your question is. Do you want to share what your expected page
result is?
To me, your code is clear as: session stores an array of string values. as
long as it is postback, the session value won't be cleaned up. So if you
enter ABC, click button and then enter DEF, click button, it will show:
ABC
DEF
what was the result you were expecting from your original impression?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。