Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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

C#
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:

C#
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:

C#
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();
    }
}


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
Posted
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 18:30pm    
Also up-voted by my 4. Please see my comment to your next question for some motivation of it. :-)
—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
 
Share this answer
 
Some answers here[^]. You should also read through the MSDN documentation.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Dec-12 18:25pm    
Sorry, none of these answers are satisfactory. Please see my answer where I explain it.
This is pretty elementary thing in practical computer science, but not many practical developers are aware of this stuff, so it needs detailed explanation. It's really important to know.

(I did not vote this time.)
—SA
R. Giskard Reventlov 28-Dec-12 4:26am    
Well, no one could ever accuse you of being modest, could they?
Sergey Alexandrovich Kryukov 28-Dec-12 12:45pm    
I don't remember it happened. Probably, I'm just right.
Why would you ask about it? :-)
—SA
R. Giskard Reventlov 28-Dec-12 13:05pm    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900