❔ IEquatable
Hello. The course I am taking is discussing interfaces. What is the point of putting IEquatable<Ticket> next to the class name below? Because I took away the IEquatable<Ticket> just to see what would happen and the Equals method still worked the way it was supposed to so what exactly is the : IEquatable<Ticket> doing and what is the point of using it?
using System;
namespace Ticket
{
public class Ticket : IEquatable<Ticket>
{
// property to store the duration of the ticket in hours
public int DurationInHours { get; set; }
// simple constructor
public Ticket(int durationInHours)
{
DurationInHours = durationInHours;
}
public bool Equals(Ticket other)
{
return this.DurationInHours == other.DurationInHours;
}
}
}
4 Replies
Stack Overflow
What's the difference between IEquatable and just overriding Object...
I want my Food class to be able to test whenever it is equal to another instance of Food. I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquat...
Essentially,
IEquatable<T>
is a more proper way of implementing equality checks
Also lets you narrow down the types to be IEquatable
in other places
Like
method, or a
onesthanks so much!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.