1 min read

Create interfaces in C# Part 2 – Create a class that implements the interface

Now create a new class Car and implement the interface.

As you can tell, if you don’t implement the interface member Equals in your new class the code is not valid.

So next add an implementation of the IEquatable<T> interface below the properties.

    class Car : IEquatable<Car>
        public string Make { get; set; }
        public string Model { get; set; }
        public string Year { get; set; }

        public bool Equals(Car car)
            return (this.Make, this.Model, this.Year) ==
                (car.Make, car.Model, car.Year);

Now in the final post we’ll create two car instances and compare them!

Leave a Reply