为什么listbox1.Items.添加我的对象的use Equals方法

本文关键字:use Equals 方法 对象 我的 listbox1 Items 添加 为什么 | 更新日期: 2023-09-27 18:19:10

我正在仔细检查Windows窗体ListBoxCollection添加方法,在分析过程中,我发现方法"add"调用了基对象中的Equals方法为什么这个方法做这个动作?


我已经使用了"调用堆栈",我找到了这个答案:

我们调用:System.Windows.Forms.ListBox.ObjectCollection。Add(object item)

它会调用:System.Windows.Forms.ListBox.ObjectCollection。AddInternal项(对象)

它会调用:System.Windows.Forms.ListBox。NativeAdd项(对象)

它会调用:System.Windows.Forms.ListControl。GetItemText项(对象)

它会调用:System.Windows.Forms.Formatter。格式化对象(对象值,System。Type targetType, System.ComponentModel.TypeConverter, sourcececonverter, System.ComponentModel.TypeConverter, targetConverter, string formatString, System.ComponentModel.TypeConverterIFormatProvider formatInfo,对象formattedNullValue,对象dataSourceNullValue)

它会调用:System.Windows.Forms.Formatter。IsNullData(object value, object dataSourceNullValue)

,最后调用:object。= (object objA, object objB)

这个方法使我的相等方法也运行:

WindowsFormsApplication1.Student。=(对象obj)

和最后一个参数将被发送到我的Equals方法(作为obj)是一个DBNULL对象!什么是DBNULL

为什么listbox1.Items.添加我的对象的use Equals方法

因为它需要能够知道哪个项目正在触发事件。如果对象是完全相同的对象,那么选择、编辑、删除就会变得困难和混乱。