关于c#对象方法的问题:Equals和GetHashCode

编程入门 行业动态 更新时间:2024-10-28 18:23:40
本文介绍了关于c#对象方法的问题:Equals和GetHashCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应用程序,可以通过电话号码按名称或姓名研究电话号码,代码是打击: 我有一个名称结构:

I have an application which can research phone number by name or name by phone number, the code are blow: I have a Name Structure:

namespace Indexers { struct Name { private string name; public Name(string text) { this.name = text; } public string Text { get { return this.name; } } public override int GetHashCode() { return this.name.GetHashCode(); } public override bool Equals(object other) { return (other is Name) && Equals((Name)other); } public bool Equals(Name other) { return this.name == other.name; } } }

电话号码结构:与姓名相同

Phone Number Structure: The same with Name

namespace Indexers { struct PhoneNumber { private string number; public PhoneNumber(string text) { this.number = text; } public string Text { get { return this.number; } } public override int GetHashCode() { return this.number.GetHashCode(); } public override bool Equals(object other) { return (other is PhoneNumber) && Equals((PhoneNumber)other); } public bool Equals(PhoneNumber other) { return this.number == other.number; } } }

电话簿课程:

A PhoneBook class:

namespace Indexers { using System; sealed class PhoneBook { private int used; private Name[] names; private PhoneNumber[] phoneNumbers; public PhoneBook() { int initialSize = 0; this.used = 0; this.names = new Name[initialSize]; this.phoneNumbers = new PhoneNumber[initialSize]; } public Name this[PhoneNumber number] { get { int i = Array.IndexOf(this.phoneNumbers, number); if (i != -1) { return this.names[i]; } else { return new Name(); } } } public PhoneNumber this[Name name] { get { int i = Array.IndexOf(this.names, name); if (i != -1) { return this.phoneNumbers[i]; } else { return new PhoneNumber(); } } } public void Add(Name name, PhoneNumber number) { enlargeIfFull(); this.names[used] = name; this.phoneNumbers[used] = number; this.used++; } // write 1st indexer here // write 2nd indexer here private void enlargeIfFull() { if (this.used == this.names.Length) { int bigger = used + 16; Name[] moreNames = new Name[bigger]; this.names.CopyTo(moreNames, 0); PhoneNumber[] morePhoneNumbers = new PhoneNumber[bigger]; this.phoneNumbers.CopyTo(morePhoneNumbers, 0); this.names = moreNames; this.phoneNumbers = morePhoneNumbers; } } } }

和一个显示搜索的窗口:

And a Window to show the search:

namespace Indexers { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void findPhoneClick(object sender, RoutedEventArgs e) { string text = name.Text; if (!String.IsNullOrEmpty(text)) { Name personsName = new Name(text); PhoneNumber personsPhoneNumber = this.phoneBook[personsName]; phoneNumber.Text = personsPhoneNumber.Text; } } private void findNameClick(object sender, RoutedEventArgs e) { string text = phoneNumber.Text; if (!String.IsNullOrEmpty(text)) { PhoneNumber personsPhoneNumber = new PhoneNumber(text); Name personsName = this.phoneBook[personsPhoneNumber]; name.Text = personsName.Text; } } private void addClick(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(name.Text) && !String.IsNullOrEmpty(phoneNumber.Text)) { phoneBook.Add(new Name(name.Text), new PhoneNumber(phoneNumber.Text)); name.Text = ""; phoneNumber.Text = ""; } } private PhoneBook phoneBook = new PhoneBook(); } }

我的问题是: 1,自< PhoneBook类中的b> Array.IndexOf 可以找到名称或数字,以确定它们是否匹配,名称和PhoneNumber中的 Equals和GetHuashCode是什么用于?为什么我需要它? 2,我没有看到代码中的任何地方调用 Equals和GetHuashCode 方法。谁和哪里称这些方法?这是因为它们是Object方法,所以会自动调用吗? 谢谢, Jay

My question is : 1, Since the Array.IndexOf in PhoneBook class can find the name or the number to know if they match or not, what is the Equals and GetHuashCode in Name and PhoneNumber used for? Why I need that? 2, I didn''t see anywhere in the code to call the Equals and GetHuashCode method. Who and where call those methods? Is this because they are Object method, so it will be called automatically? Thanks, Jay

推荐答案

为什么通过覆盖方法更改对象标识比较等于还需要覆盖 GetHashCode 不是一个微不足道的问题。我非常熟悉那些很困惑的开发人员。我在过去的答案中解释了这个问题;请参阅: Object.GetHashCode()方法C#.Net [ ^ ]。
-SA
Why the change of object identity comparison by overriding of the method Equals also requires overriding of GetHashCode is not a trivial question. I knew pretty experience developers who were much confused. I explain this matter in my past answer; please see: Object.GetHashCode() Method in C#.Net[^].
—SA

一些答案​​在哪里 [ ^ ]。您还应该阅读MSDN文档。 Some answers here[^]. You should also read through the MSDN documentation.

更多推荐

关于c#对象方法的问题:Equals和GetHashCode

本文发布于:2023-11-09 10:51:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   方法   GetHashCode   Equals

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!