简体   繁体   English

为什么这两个清单 <string> 总是包含相同数量的项目?

[英]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. 我有两种形式:Main和Log。 The Main form has a button that adds to List _debugLog when clicked. Main窗体中的按钮在单击时会添加到List _debugLog。 When btnDebug is clicked, it opens the Log form, passing _debugLog to it. 单击btnDebug时,它将打开Log表单,并将_debugLog传递给它。 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. 每2.5秒触发一次事件log.UpdateLog(),以使用更新后的日志来更新Log表单。 However, mainFormLog.Count and _log.Count are always the same and they BOTH increase when btnAdd is clicked on the main form. 但是,mainFormLog.Count和_log.Count始终相同,并且当在主窗体上单击btnAdd时,它们两者都会增加。 How does _log have the new _debugLog (mainFormLog) from the tick event? _log如何从tick事件中获取新的_debugLog(mainFormLog)?

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. 好吧,您是将对列表的引用从Main传递到Log,因此它实际上是相同的列表。

If you want a separate list that gets initialized with the list from Main you can use: 如果您想要一个单独的列表,并使用Main中的列表进行初始化,则可以使用:

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. 对于引用类型,该值是一个引用,该引用可以为null,也可以是导航到包含该信息的对象的方式。

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. 它可能上面写有值“ 5”或“ false”,但它不能拥有我的房子……它必须要有指向我房子的指示。 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. 您的所有变量_debugLogmainFormLog_log都指向内存中的同一列表。 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. 您只创建了一个列表,当您向该列表分配新变量时,它只是指向内存中某个位置的指针,它不会自动创建该列表的新副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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