Why must the base class be specified before interfaces when declaring a derived class?

后端 未结 7 1742
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 06:54
public interface ITest
{
    int ChildCount { get; set; }
}

public class Test
{
}

public class OrderPool : ITest, Test
{
    public int ChildCount
    {
        ge         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 07:09

    C# supports only single inheritance, but allows classes to implement multiple interfaces. That being the case, it's much clearer to always have the base class specified in the same place by convention, rather than mixed in with a bunch of interfaces.

    Regardless of convention, the specification mandates that this is the case anyway, which is why you're seeing that error.

    Remember, there's nothing in the specification that says all of your interfaces have to be named with a capital "I". - that's just convention. So if your class implemented interfaces that didn't follow that convention, and if the specification allowed you to specify the base class and interfaces in any order, you wouldn't be able to easily tell which identifier was the base class and which were interfaces. Example:

    class MyDerivedClass : A, B, C, D, E   // which is the base class?  
    {
    ...
    }
    

提交回复
热议问题