c# - 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
}
解决方案
你应该考虑How to compare 2 object in c#
,有一些方法可以实现这一点
- 实施IEquatable
<T>
- 序列化两个对象,然后将它们作为字符串结果进行比较
顺便说一句,这个链接对你有帮助。
推荐阅读
- javascript - 即使填写了正确的凭据,验证也不起作用
- jquery - 下拉列表未显示在剑道网格 detailInit 网格的所有行中
- java - 从数组中获取列值
- reactjs - 无法获取组件状态
- mysql - 是否可以按给定表中的两个字段排序,一个按升序排列,另一个按降序排列?
- java - Spring Boot 从请求中解析无效日期
- spy - .msc 间谍 BluePrism
- matlab - MATLAB:如何遍历 MATLAB 中的每个 (x,y) 对?
- pentaho - Pentaho - PDI:获取流字段
- docker - 本地 Docker 连接到 Kubernetes 集群