首页 > 解决方案 > List 中的控件不等同于原始控件

问题描述

因此,我创建了一个列表(panelList)并插入了 10 个面板控件(已在 aspx 设计页面上创建)。但是,当我尝试检查 panelList[0] 是否与 Panel1 相同的面板(其中是我在 panelList 中添加的第一个面板),我返回 FALSE ...知道为什么吗?这是我的代码


 static List<Panel> panelList = new List<Panel>();
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack) 
        {
            panelList.Add(Panel1);
            panelList.Add(Panel2);
            panelList.Add(Panel3);
            panelList.Add(Panel4);
            panelList.Add(Panel5);
            panelList.Add(Panel6);
            panelList.Add(Panel7);
            panelList.Add(Panel8);
            panelList.Add(Panel9);
            panelList.Add(Panel10);
         } 
  }  

   protected void AddQuestionButton_Click(object sender, EventArgs e)
      {    
       Debug.WriteLine(panelList[0].Equals(Panel1));
      // here i get returned false in the debug output
      }

标签: c#asp.netlist

解决方案


你应该考虑How to compare 2 object in c#,有一些方法可以实现这一点

  1. 实施IEquatable<T>
  2. 序列化两个对象,然后将它们作为字符串结果进行比较

顺便说一句,这个链接对你有帮助。


推荐阅读