【问题标题】:why is my class array element behaving like a pointer? (C#)为什么我的类数组元素表现得像一个指针? (C#)
【发布时间】:2025-12-06 03:45:02
【问题描述】:

我有这个简短的 Windows 窗体应用程序代码,我无法正常工作。它使用类的实例(作为临时存储位置)来填充相同类类型的数组。它有一个按钮和一个文本框。如果按下按钮,文本将保存在 Name 成员中并添加到数组中。 如果 Name 被另一个数组成员占用,它应该显示一条消息“名称被占用!”而不是将其添加到数组中。问题是这个条件检查总是正确的! 如果我们输入“Marry”,调试器会显示

 if (examleArray[i].Name == temp.Name)

相当于:

if ("Marry" == "Marry")

好像一个指向另一个。为什么是这样?我如何解决它?谢谢!

namespace errorexample
{
    public partial class Form1 : Form
    {

        example temp = new example();
        example[] examleArray = new example[10];
        int examleArrayIndex = 0;
        bool NameTaken = false;

        public Form1()
        {  InitializeComponent();  }

        private void button1_Click(object sender, EventArgs e)
        {
            temp.Name = textBox1.Text;         
            NameTaken = false;

            for (var i = 0; i < (examleArrayIndex); i++)
            {
                if (examleArray[i].Name == temp.Name)
                {
                    NameTaken = true;
                    MessageBox.Show("Name taken!");
                }

            }

            if (NameTaken == false)
            {
                examleArray[examleArrayIndex] = temp;
                examleArrayIndex++;
            }
        }
    }

    public class example {
        public string Name;  
    }

}

【问题讨论】:

  • 因为您的表单中只有一个example 实例,所以您继续重复使用它
  • 因为你有一个引用数组,它们都指向同一个 example 对象,因为你只创建了一个。

标签: c# arrays class pointers


【解决方案1】:

在点击事件中移动临时对象初始化,否则你正在更新相同的对象

private void button1_Click(object sender, EventArgs e)
        {
            example temp = new example();
            temp.Name = textBox1.Text;         
            NameTaken = false;

            for (var i = 0; i < (examleArrayIndex); i++)
            {
                if (examleArray[i].Name == temp.Name)
                {
                    NameTaken = true;
                    MessageBox.Show("Name taken!");
                }

            }

            if (NameTaken == false)
            {
                examleArray[examleArrayIndex] = temp;
                examleArrayIndex++;
            }
        }

你可以使用列表

List<string> example =
        new List<string>();

if(example.Contains(textBox1.Text))
{ 
  MessageBox.Show("Name taken!");
}else
{
  example.Add(textBox1.Text);
}

【讨论】:

    【解决方案2】:

    您只有一个temp 对象,并且您不断添加它到数组中。这是相同的temp,因为您从未创建过new。我已经为你重写了:

     List<example> examleArray = new List<example>();
     private void button1_Click(object sender, EventArgs e)
     {
       if (examleArray.Any(e=>e.Name == textBox1.Text))
       {
         MessageBox.Show("Name taken!");
       } else {
         examleArray.Add(new example { Name = textBox1.Text });
       }
     }
    

    我还将您的固定数组转换为列表,这样您就不会意外尝试添加 11 个名称然后炸毁。为了简化,我已将您的搜索转换为 LINQ。

    【讨论】:

      【解决方案3】:

      您只创建一个example 对象。您将对同一对象的多个引用放入数组中。一种解决方案是创建一个新的example 对象以放入数组中:

      public class example {
          public string Name;
          public example( string name ){
              Name = name;
          }
      }
      
      //...
      examleArray[examleArrayIndex] = new example( temp.Name );
      

      【讨论】:

        最近更新 更多