title: multiple instances of C# DataTable stored in session ..how this reference works?
body: <p>I have a problem with <code>DataTable</code> stored in <code>Session</code> and code execution when retrieving it from this <code>Session</code>. Maybe this isn't a problem and is just me wrongly understanding how instances and references really work.</p>
  
  <p>I have a POCO class <code>User</code> where I store all the data of logged in user. The POCO Class look like this:</p>
  
  <pre><code>public class User 
  {
     public int UserID { get; set; }
     public string UserFirstName { get; set; }
     public string UserLastName { get; set; }
     public DataTable UserRights { get; set; }
  }
  </code></pre>
  
  <p>Once the user is logged in (on login page) I create an instance of <code>User</code> class and I store all retrieved data from login into <code>Session["User"]</code>:</p>
  
  <pre><code>User _user = new User();
  _user = MyLogin.GetUser() // method returns filled User object with data from db
  Session["User"] = _user; // I store it into session
  </code></pre>
  
  <p>Then on my pages when I need data from Session for user rights (stored in datatable) I look into session and retrieve DataTable:</p>
  
  <pre><code>User _currUser = (User)Session["User"];
  DataTable dt = new DataTable();
  dt = _currUser.UserRights;
  </code></pre>
  
  <p>This code works OK, but some strange behavior starts when I look into Session for DataTable UserRights <strong>multiple times on the same page</strong>. </p>
  
  <p>Example: I have two user controls on page and in each one of them I look into session for DataTable like in the code above. Then if I change DataTable object instance in one of the user controls code like this for example:</p>
  
  <pre><code>dt.Columns.Remove("RoleID");
  </code></pre>
  
  <p>...this change impacts the instance in second user control too(??). And the same goes if give instances different names:</p>
  
  <pre><code>// FirstUserControl.ascx.cs
  User _currUser1 = (User)Session["User"];
  DataTable dt1 = new DataTable();
  dt1 = _currUser1.UserRights;
  dt1.Columns.Remove("RoleID")
  
  // SecondUserControl.ascx.cs
  User _currUser2 = (User)Session["User"];
  DataTable dt2 = new DataTable();
  dt2 = _currUser2.UserRights; &lt;--- dt2 is without column "RoleID" too!? 
  </code></pre>
  
  <p>Can somebody in a clear way explain to me why this is happening and how instances and references works in this example (it looks I don't understand them correctly). I always thought that if I execute code like the one above I would get <code>dt1</code> and <code>dt2</code> <code>DataTable</code> object which will not have anything in common when processing them.</p>