C# interface inheritance (can inherit how many other interfaces)

I understand that

a class can inherit multiple interfaces and

a abstract class can inherit from another class  and one or more interfaces

How bout interface inheritance,

Can an interface inherit from multiple interfaces?

Answers


Yes, it can. Look at ICollection<T>:

public interface ICollection<T> : IEnumerable<T>, IEnumerable

An interface is not inherited it is implemented, so it is not involved in the 'single inheritence' rule. Anything that can implement an interface can implement multiple interfaces.

The slightly confusing thing - and the answer to your question - is that an interface can implement other interfaces. Effectively the interface inherits all of the characteristics of every interface it implements:

interface IHasPosition
{
    float X { get; }
    float Y { get; }
}

interface IHasValue<T>
{
    T Value { get; }
}

interface IPositionValue<T> : IHasPosition, IHasValue<T>
{ }

Rather than being simply an empty interface, IPositionValue<T> has all three properties from the two interfaces it implements. When creating a class that implements IPositionValue<T>, the class automatically implements the interfaces that IPositionValue<T> implements:

class StringAtLocation : IPositionValue<string>
{
    public float X { get; set; }
    public float Y { get; set; }
    public string Value { get; set; }
}

static void Main()
{
    StringAtLocation foo = new StringAtLocation { X = 0, Y = 0, Value = "foo" };
    // All of the following are valid because of interface inheritence:
    IHasPosition ihp = foo;
    IHasValue<string> ihv = foo;
    IPositionValue<string> ipv = foo;       
}

And no, it's not documented in the interface keyword documentation or the Interfaces (C# Programming Guide) section on MSDN. Sadly.


Need Your Help

R checking a parameter is defined

r function parameter-passing

I'm looking for a general practice how to check a parameter was defined in a function.

Can't get code to work in html file but will work on jsfiddle

javascript jquery html jsfiddle

I am trying to use this code in my html file. It will work if I use the link http://jsfiddle.net/eqkmv/1/show/ however the javascript doesn't work when I try to View Page Source and copy/paste the ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.