简体   繁体   中英

Why do these two List<string> always contain the same number of items?

I have a very simple program that for some reason has me stumped. I put it down, came back at it again this morning and I'm still stumped. First off, I'm aware this is not an ideal solution. I have two forms: Main and Log. The Main form has a button that adds to List _debugLog when clicked. When btnDebug is clicked, it opens the Log form, passing _debugLog to it. Everything is fine, the timer is setup and runs, everything is normal. The event log.UpdateLog() is triggered every 2.5 seconds to update the Log form with the updated log. However, mainFormLog.Count and _log.Count are always the same and they BOTH increase when btnAdd is clicked on the main form. How does _log have the new _debugLog (mainFormLog) from the tick event?

namespace Tool
{
    public partial class Main : Form
    {
        private List<string> _debugLog = new List<string>();

        public Main()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            _debugLog.Add("message!");
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            Log log = new Log(_debugLog);
            log.Show();

            Timer dt = new Timer();
            dt.Interval = 2500;
            dt.Enabled = true;

            dt.Tick += delegate {
                log.UpdateLog(_debugLog);
            };

        }
    }

    public partial class Log : Form
    {
        private List<string> _log;

        public Log(List<string> log)
        {
            InitializeComponent();
            _log = log;
        }

        public void UpdateLog(List<string> mainFormLog)
        {
            if (mainFormLog.Count > _log.Count)
            {
                MessageBox.Show("Log has been updated!");
            }
            else
            {
                MessageBox.Show("Nothing new!" + mainFormLog.Count.ToString() + " / " + _log.Count.ToString());
            }
        }
    }
}

Well, you're passing the reference to the list from Main to Log, so it's actually the same list.

If you want a separate list that gets initialized with the list from Main you can use:

public Log(List<string> log)
{
    InitializeComponent();
    _log = new List<string>(log);
}

Maybe this helps to understand the difference between variables and references:

For a value type, the value is the information itself. For a reference type, the value is a reference which may be null or may be a way of navigating to an object containing the information.

For example, think of a variable as like a piece of paper. It could have the value "5" or "false" written on it, but it couldn't have my house... it would have to have directions to my house. Those directions are the equivalent of a reference. In particular, two people could have different pieces of paper containing the same directions to my house - and if one person followed those directions and painted my house red, then the second person would see that change too. If they both just had separate pictures of my house on the paper, then one person colouring their paper wouldn't change the other person's paper at all.

All your variables _debugLog , mainFormLog , and _log are pointing to the same list in memory. You've only created one list, and when you assign a new variable to that list, it's just a pointer to some location in memory, it doesn't automatically create a new copy of the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM