• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 44
  • Last Modified:

interface hierarchy in C#

I am trying to understand use cases where one would use interface hierarchy, something like below.

As my question sounds vague, if anyone can provide me, when and why someone would use this, with a real world example and explanation, that would be great.

public interface A{
}

public interface B : A {
}

public interface C : B{
}
0
novice programmer
Asked:
novice programmer
2 Solutions
 
Chris StanyonCommented:
Take a look at the collection in .Net:

Interface IEnumerable
    GetEnumerator()

Interface ICollection: IEnumerable
    CopyTo()

Interface IList: ICollection
    Add()
    Remove()
    ....

A concrete implementation of an IList will have the methods from all 3 interfaces. A concrete implementation of an ICollection, will only need to have the members from IEnumerable and ICollection. Gives a nice clean way to re-use code.
0
 
Dustin SaundersDirector of OperationsCommented:
An interface is like an abstract class, except that you can inherit multiple interfaces.  It allows you to set a requirement of a method with the expectation that the method will always be there.  For example:

class Program
    {
        public interface IThrowable
        {
            void ThrowItem();
        }

        public interface IExplosive : IThrowable
        {
            void Explode();
        }

        class Grenade : IExplosive
        {
            bool Disarmed { get; set; } = false;
            int Timer { get; set; } = 10;
            bool Exploded { get; set; } = false;

            void Throw()
            {
                while (Timer > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    Timer--;
                }
                Explode();
            }

            public void Explode()
            {
                this.Exploded = true;
            }

            public void Disarm()
            {
                Disarmed = true;
            }

            public void ThrowItem()
            {
                if (!Disarmed)
                    Throw();
            }
        }

        class Knife : IThrowable
        {
            bool Thrown { get; set; } = false;

            public void ThrowItem()
            {
                Thrown = true;
            }
        }

        static void Main(string[] args)
        {
            List<IThrowable> weapons = new List<IThrowable>
            {
                new Knife(),
                new Grenade()
            };

            foreach (IThrowable weapon in weapons)
            {
                weapon.ThrowItem();
            }
        }
    }

Open in new window


The grenade is much more complex, but I can put these together as they inherit the same base interface and know that each one can be thrown via ThrowItem();

The multiple hierarchies just extend that functionality.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now