Asp.net会话通过ref或val分配值 [英] Asp.net Session assign value by ref or by val
问题描述
我需要提问
是通过val或ref
进行的会话工作
意思是如果我说
iam在网站上工作,并使用linq转换为sql dbml
我已将存储过程存储在dbml
例如说我的存储过程叫sp
在我的C#代码中
1-说sesssion ["a"] = list< sp>
2-list< sp> ;.添加新行
3-当我再次查看会话时,我发现它的价值增加了
而不是我没有在添加新行后再次分配它
i need to ask question
is session work by val or by ref
mean if i said
iam working on web site and use linq to sql dbml
i have stored procedure in dbml
for example say my stored procedure called sp
in my c# Code
1- say sesssion["a"]=list<sp>
2- list<sp>.add new row
3- when i look again on my session i find it''s value increase
instead of i didn''t assign it it again after add the new row
推荐答案
为什么不简单地尝试一下呢?
会话将项目存储为SessionStateItemCollection
.
反过来,这是对象的集合.
因此,我认为会话中的项目将是引用类型.
Why don''t you simply try it out?
A session stores items as aSessionStateItemCollection
.
This, in turn, is a collection of objects.
So I would think items in a session will be reference type.
session["a"]
将为1.
至于您更新的(第二个)问题,sesssion ["a"]中的列表将与您添加一行的列表相同.
请参阅
值类型(C#参考) [引用类型(C#参考) [
session["a"]
will be 1.
As for your updated (second) question, the list in sesssion["a"] will be the same list you added a row to.
Refer to
Value Types (C# Reference)[^]
Reference Types (C# Reference)[^]
If you don''t want the list in your session to get the row you add you can do
Session["a"] = new List<...>(list);
是如果您将其放入页面加载方法中,就足够简单地尝试一下:
It is simple enough to try out, if you put this into the page load method:
int x =2;
Session["a"] = x;
x = 3;
var y = Session["a"];
y的最终值为2,使会话引用类型成为项.
y ends up with the value 2, making items in the Session reference types.
这篇关于Asp.net会话通过ref或val分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!