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

  • Home
  • SEARCH
  • 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 3323100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:15:43+00:00 2026-05-17T23:15:43+00:00

Note the following code: Control foo = null; Control bar = null; int i

  • 0

Note the following code:

Control foo = null;
Control bar = null;
int i = 0;

protected void Page_Load(object sender, EventArgs e)
{
    test();
    test();
    Page.Controls.Add(foo);
}

void test()
{
    i++;
    bar = new Control();
    bar.Controls.Add(new LiteralControl(i.ToString()));
    if (foo == null)
    {
        foo = new Control();
        foo.Controls.Add(bar);
    }
}

When trying out the above code, I was suprised to see the result printed is ‘1’ (not ‘2’).

Im assuming this is because when i’m adding the control bar to foo, foo.Controls.Add() resolves the reference bar, rather than just storing the reference itself.

1) Can anyone confirm this is the case, or possibly elaborate?

2) I have a feeling if I was allowed to do foo.Controls.Add(ref bar); it would show ‘2’, but obviously that syntax is illegal. Is it possible for this to be the case without major refactoring?

  • 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-05-17T23:15:44+00:00Added an answer on May 17, 2026 at 11:15 pm

    Jon Skeet’s answer is good (of course), but I just want to emphasize one point:

    Variables of reference type (such as bar and foo) don’t contain objects, they contain references to objects.

    So the first time through the test() method, bar contains a reference to a Control which has a 1 in it. And that reference gets added to foo.

    The second time through the test() method, bar contains a reference to a different Control, which has a 2 in it. And that reference doesn’t get added to foo.

    Why is it a different reference the second time? Because at the start of test() you say bar = new Control(); That stores a reference to a new Control in bar. From there on, bar has nothing to do with the Control it previously referred to, although a reference to that control still lives on in foo’s Controls collection.

    If you want to change your code so it behaves how you expected, you could do this:

    Control foo = null;
    Control bar = new Control();
    int i = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        test();
        test();
        Page.Controls.Add(foo);
    }
    
    void test()
    {
        i++;
        bar.Controls.Clear()
        bar.Controls.Add(new LiteralControl(i.ToString()));
        if (foo == null)
        {
            foo = new Control();
            foo.Controls.Add(bar);
        }
    }
    

    In that case, we only store a new reference in bar one time, so when all is said and done it should still match the reference we added to foo.

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

Sidebar

Related Questions

Take note of the following C++ code: #include <iostream> using std::cout; int foo (const
Environment is VS2008, .Net 3.5 The following C# code (note the specified encoding of
I'm running the following code to trace the tagName of every control that gets
I have the following HTML (note the CSS making the background black and text
We're seeing the error message ORA-00936 Missing Expression for the following SQL: Note that
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
I'm using the following code to override a JavaScript function named dismissRelatedLookupPopup(). In Firefox,
I have the following code I am trying to fix foreach (System.IO.DirectoryInfo dir in

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.