Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialshawn khah
15,082 Pointscan't solve challenge 1 of 1
Challenge Task 1 of 1
Add a GetHashCode method to the VocabularyWord class that returns an appropriate hash code. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Preview Get Help Check Work VocabularyWord.cs
1 using System; 2 namespace Treehouse.CodeChallenges 3 { 4 public class VocabularyWord 5 { 6 public string Word { get; private set; } 7 8 public VocabularyWord(string word) 9 { 10 Word = word; 11 } 12 13 public override string ToString() 14 { 15 return Word; 16 } 17 18 public override bool Equals(object obj) 19 { 20 if(!(obj is VocabularyWord)) 21 { 22 return false; 23 } 24 25 var that = obj as VocabularyWord; 26 27 return this.Word.Equals(that.Word); 28 } 29 } 30 } 31
using System;
namespace Treehouse.CodeChallenges
{
public class VocabularyWord
{
public string Word { get; private set; }
public VocabularyWord(string word)
{
Word = word;
}
public override string ToString()
{
return Word;
}
public override bool Equals(object obj)
{
if(!(obj is VocabularyWord))
{
return false;
}
var that = obj as VocabularyWord;
return this.Word.Equals(that.Word);
}
}
}
4 Answers
Chris Adamson
132,143 PointsThe base class GetHashCode is declared as:
public virtual int GetHashCode();
For this exercise, since Word is the only member variable, it's easiest to just return the hashcode of the Word member:
public override int GetHashCode() {
return this.Word.GetHashCode();
}
shawn khah
15,082 Pointsthank's chris
Andrei Li
34,486 PointsI typed the same but it hasn't worked. Only copy/paste helped.
Andrei Li
34,486 PointsI have found typing error in my code. Everything is perfect.