我有一个应用程序,可以通过电话号码按名称或姓名研究电话号码,代码是打击: 我有一个名称结构:
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 [ ^ ]。
一些答案在哪里 [ ^ ]。您还应该阅读MSDN文档。 Some answers here[^]. You should also read through the MSDN documentation.
更多推荐
关于c#对象方法的问题:Equals和GetHashCode
发布评论