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{
}
mikha eAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.