//重写equals方法 public override bool Equals(object obj) { InforMation i = obj as InforMation; if (this.Name == i.Name && this.Gender == i.Gender && this.Id == i.Id && this.Age == i.Age) { return(true); } else { return(false); } }
private void button1_Click(object sender, EventArgs e) { bool islogin = false; //获取输入的值,并且匹配 #region 通过多个if嵌套解决 //for (int i = 0; i < list.Count; i++) //{ // if (txtID.Text.Trim() == list[i].Id.ToString()) // { // if (txtName.Text.Trim() == list[i].Name.ToString()) // { // if (txtAge.Text.Trim() == list[i].Age.ToString()) // { // if (RadMain.Checked) // { // if (RadMain.Text == list[i].Gender.ToString()) // { // MessageBox.Show("登录成功!"); // islogin = true; // break; // } // } // else if (radWoman.Checked) // { // if (radWoman.Text == list[i].Gender.ToString()) // { // MessageBox.Show("登录成功!"); // islogin = true; // break; // } // } // } // } // } //} #endregion #region 通过重写Information的Equals解决 try { InforMation preson = new InforMation(); preson.Age = Convert.ToInt32(txtAge.Text.Trim()); preson.Name = txtName.Text.Trim().ToString(); preson.Id = Convert.ToInt32(txtID.Text.Trim()); preson.Gender = RadMain.Checked ? '男' : '女'; for (int i = 0; i < list.Count; i++) { if (list[i].Equals(preson)) { MessageBox.Show("登录成功!"); islogin = true; } } #endregion if (!islogin) { MessageBox.Show("登录失败!"); } } catch { } }