测试listBox1.Items.Remove方法是利用ToString还是利用gethashCode来定位元素的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testCombox
{



    public partial class Form1 : Form
    {
        test t1 = new test("t1", 1);
        test t2 = new test("t2", 2);
        test t3 = new test("t3", 3);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Add(t1);
            this.listBox1.Items.Add(t2);
            this.listBox1.Items.Add(t3);
        }
        /// <summary>
        /// 判断根据Object是否能够删除t2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Remove(t2);//删除成功,下面测试listBox1.Items.Remove
                                           //方法是利用ToString还是利用gethashCode来定位元素的
        }
        /// <summary>
        /// 判断listBox1.Items.Remove(object)是不是根据Object的Tostring方法来删除元素的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            t2.Name = "t22";                //修改元素的名称,看是否能够被删除。
            this.listBox1.Items.Remove(t2);//删除成功,证明Remove方法可能是根据getHashCode来删除元素的

        }
        
    }


    /// <summary>
    /// 一个测试类
    /// </summary>
    public class test
    {
        public string Name { get; set; }
        public override string ToString()
        {
            return this.Name;
        }
        public int testInt = 0;
        public test(string name, int num)
        {
            this.Name = name;
            testInt = num;
        }
      
    }

}

  代码下载

你可能感兴趣的:(HashCode)