Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Interfaces providing default Interface Implementation

If I have:

public interface IValueStorage : IComparable<IValueStorage>
{
    public int compareObject {get;}
    public int CompareTo(IValueStorage other)
    {
        return compareObject.CompareTo(other.compareObject);
    }
}

Why do I need to redefine the interface implementation of IComparable on any classes that implement this interface? Can I declare each member use the base implementation?

I would ordinarily just change this interface to an abstract class, but on this occasion, the implementers of this interface are all (and should remain) structs.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

IService.CompareTo does not override IComparable<IService>.CompareTo; it creates a new method that your structs must implement. In order to have an interface method override another interface’s method, you must use the explicit interface implementation syntax:

public interface IService : IComparable<IService>
{
    public int compareObject { get; }
    int IComparable<IService>.CompareTo(IService other)
    {
        return compareObject.CompareTo(other.compareObject);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading