❔ 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
Angius
Angius2y ago
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...
Angius
Angius2y ago
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
public void Foo(IEquatable something)
public void Foo(IEquatable something)
method, or a
public void Bar<T>() where T : IEquatable
public void Bar<T>() where T : IEquatable
ones
DustinMarino
DustinMarinoOP2y ago
thanks so much!
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server