Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:06:41+00:00 2026-06-06T10:06:41+00:00

Time to learn some basics. Look at the code below: protected void Button1_Click(object sender,

  • 0

Time to learn some basics.

Look at the code below:

  protected void Button1_Click(object sender, EventArgs e)
 {

    List<string> a;
    if(Session["data"] == null)
    {
        a = new List<string>();
        a.Add("abc");
        a.Add("def");
        a.Add("ghi");

        Session["data"] = a;
    }
    else
    {
        a = (Session["data"] as List<string>);
    }
    a.Add("jkl");
    foreach (string s in a)
    {
        lblTest.Text += s + "<br />";
    }
}

There is nowhere else in the codebehind where I am doing anything with sessions. The expected behaviour of this code is that in the first run(click) session will hold a reference to “a”. But “a” has local scope in the above function, so somewhere the value of “a” must be copied into the session. Where does that happen? Does it happen before the function gets executed because anywhere else that happens(i.e Page unload, validate, render) the List “a” will not be available. So when and where does the value assigned or referenced gets stored in the session?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:06:45+00:00Added an answer on June 6, 2026 at 10:06 am

    a is a reference to your instance of List<string>. When you assign Session["data"] = a, it now also has a reference to that same instance of List<string>. So despite the fact that a goes out of scope, that list instance remains since at least one reference remains. Accessing Session["data"] will continue to return a reference to that same instance until it is removed from the session (or replaced by something else).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have spend quite some time to learn Sencha touch, just like an evaluation about
I am new to iOS programming and try to learn some of the basics
I'm trying to learn some of the basics of developing OS X apps with
Quite some time i`ve learnt MVC. I have seen various techniques to post data
For some time I am worknig in MVC, I learnt from this post, http://evolpin.wordpress.com/2011/04/12/asp-net-mvc-partialview-with-ajax/#comment-435
I need to learn RegEx but don't have time to figure this out right
I want to learn .NET and I have 2 weeks time of this. I
im just starting to learn LINQ and at the same time im working on
I am just starting to learn Ruby (first time programming), and have a basic
I am trying to learn Visual C++ 2008 and C++ at the same time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.