Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I modify ASP.NET session object this way?

Tags:

asp.net

Imagine that I have an instance (oEmp) of "Employee" class and I would like to store it session.

Session["CurrentEmp"] = oEmp;

If I modify a property in oEmp as follows:

oEmp.Ename = "Scott";

Am I referring to session item through above statement or just only "oEmp"?

Session["CurrentEmp"] = oEmp; //Do we still need this after any property is modified

Is that the same case, if I opted for SQL Server session state (instead of InProc).

thanks

like image 670
user203687 Avatar asked Dec 21 '22 06:12

user203687


2 Answers

Asp.net Session will hold the reference, so you shouldn't need to do the following:

Session["CurrentEmp"] = oEmp;

after modifying oEmp;

like image 67
Joe Ratzer Avatar answered Jan 21 '23 05:01

Joe Ratzer


Session Variables are held as reference types so there is no need to update its value every time.
You object instance that you store, only the reference to that object is stored in the session variable.

Here are some link to help you find more details

http://bytes.com/topic/asp-net/answers/447055-reference-types-session

http://forums.asp.net/t/350036.aspx/1

Do asp.net application variables pass by reference or value?

like image 34
Guru Kara Avatar answered Jan 21 '23 03:01

Guru Kara