-1

I need to explicitly implement standard c# interfaces such as (IComparable, IComparer, IEquatable, IEnumerable, IEnumerator). Am I doing it right?

 class Lemon : IComparable
    {
        public String name { get; set; }

        public int id { get; set; }
        public Lemon (String name, int id)
        {
            this.name = name;
            this.id = id;
        }

        int IComparable.CompareTo(object obj)
        {
            Lemon other = (Lemon)obj;
            if (this.id > other.id)
                return 1;
            else if (this.id < other.id)
                return -1;
            else return 0;
        }

        public void diamond ()
        {
            Console.WriteLine();
        }

        public override string ToString()
        {
            return this.name + " " + this.id;
        }
    }

and now main :

static void Main(string[] args)
        {
            List<IComparable> icL = new List<IComparable>();
            IComparable temp = new Lemon("Git", 99);
            icL.Add(temp);
            icL.Add(new Lemon("Green", 9));
            icL.Add(new Lemon("Don", 7));
            icL.Add(new Lemon("Simon", 12));

            icL.Sort();

            foreach (IComparable itm in icL)
            {
                Console.WriteLine(itm.ToString());
            }


            Console.WriteLine("----------");

        }

So what do you think?

And another problem is how could I access method diamond when I am iterating through the collection ?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
uvytautas
  • 518
  • 8
  • 18
  • It might be worth looking at implementing IComparable is opposed to IComparable. Your CompareTo method would take a parameter of type Lemon, and you could simply return id.CompareTo(other.Id) – Max Hampton Oct 14 '14 at 18:22
  • 1
    It's really dangerous to make types that implement `IEquatable` mutable. There are a few other really peculiar things about your code; it would help if you told us what you are [trying to accomplish](http://meta.stackexchange.com/questions/66377/), not just what you are doing, because what you are doing makes no sense. – Dour High Arch Oct 14 '14 at 18:26
  • @DourHighArch this is just an example, I am making other project and one of its requirements is to explicitly implement one of standart .NET interfaces – uvytautas Oct 14 '14 at 18:29

1 Answers1

1

There is no single answer to this; it is, however, useful for types that are comparable to implement IComparable<T>. This is usually a case of sharing a single implementation:

    int IComparable<Lemon>.CompareTo(Lemon obj) {
        return CompareTo(obj);
    }
    int IComparable.CompareTo(object obj) {
        return CompareTo(obj as Lemon);
    }
    private int CompareTo(Lemon obj) {
        return obj == null ? -1 : (this.id - obj.id);
    }

Note that implementing IEnumerable/IEnumerator is very different, and is usually done via simply returning an encapsulated object's iterator, or by writing an iterator block; it is very rare to manually write an iterator from scratch.

If you implement IEquatable<T>, you should ensure that object.Equals, object.GetHashCode and IEquatable<T>.Equals all have matching implementations; for the 2 Equals methods, this can again be done by having a single method that you call from both.

Your list could be a List<Lemon>; it should still sort correctly. This will also make it easy to access methods like diamond():

var icL = new List<Lemon>();
// ...
foreach (var item in icL) {
    item.diamond();                
    Console.WriteLine(item.ToString());
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • but do i need to create Interface variables in main? Like IComparable temp = new Lemon("Git", 99); – uvytautas Oct 14 '14 at 18:31
  • @user3662708 that depends; do you want to call any methods on those interfaces that aren't on the public API? your code *doesn't*, so `Lemon temp = new Lemon(...)` or `var temp = new Lemon(...)` is fine. – Marc Gravell Oct 14 '14 at 18:39
  • I need to implement IComparable explicitly. So Lemon temp = new Lemon(...) is not explict – uvytautas Oct 14 '14 at 18:41
  • @user3662708 that has nothing whatsoever to do with whether or not `IComparable` is implemented explicitly. In my example, it will work fine, despite using explicit interface implementation. – Marc Gravell Oct 14 '14 at 18:42
  • It is problem that I am making project and one of the requirements is to implement standart interface explicitly – uvytautas Oct 14 '14 at 18:47
  • it would be great to see a great example of implementing standart .NET interface explicitly – uvytautas Oct 14 '14 at 18:52
  • @user3662708 *What* is a problem? again: the code in my answer *only* uses explicit interface implementation, and works fine. So what **exactly** are you saying is a problem? Please be very specific. – Marc Gravell Oct 14 '14 at 18:53
  • @user3662708 also; the word is "standard" – Marc Gravell Oct 14 '14 at 18:54
  • I am sorry @MarcGravell for my stupidity, I thought that only for example 'IComperable tp = new Lemon(..)' would be called expicit interface implementation – uvytautas Oct 14 '14 at 18:59
  • @user3662708 no; that is not what "explicit interface implementation" means; "explicit interface implementation" simply means that the interface's methods are not on the type's public API; for example, if you could do `Lemon foo = ...; int delta = foo.CompareTo(anotherLemon);` then it would **not** be "explicit interface implementation". However, you **cannot** do this. – Marc Gravell Oct 14 '14 at 19:02