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++;
}
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
{
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();
}
}
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