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 trial

C# Intermediate C# System.Object Object.Equals

Override compiler error. When included: nothing to override; when left off: two methods named the same? What am missing?

I must be missing something simple. I am new to C#, but I have experience with JS and Python. Please help.

The challenge is to create an Equals() method that compares the input to the Word and return true or false. When I try to override it says there is nothing to override, when I leave it off it says there are two methods with the same name. What to do?

VocabularyWord.cs
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(string word)
        {
            if(Word == word)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,268 Points

The reason the compiler finds "no suitable method found to override" is because your method has a different signature. An override must have the same method signature as what it is overriding.

The built-in virtual "Equals" takes an object argument.

Bonus hint: you can expect to type cast your argument so you can use the membership operator on it.

Thank you. I am still new to C# and some of the technical aspects of the base concepts aren't clearly stated in the videos. Your answer makes it clear.