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

public interface ITest { int ChildCount { get; set; } } public class Test { } public class OrderPool : ITest, Test { public int ChildCount { get; set; } } 

The error says that the base class "Test" should be present before any interfaces. Why is it necessary to expand the class first and then implement inteface?

+6
source share
7 answers

Because the specification says so in Β§17.1.2.

+16
source

C # supports only one inheritance, but allows classes to implement multiple interfaces. In this case, it is much clearer to always have a base class defined in one place by convention, and not mixed with a bunch of interfaces.

Regardless of the agreement, the specification requires this to be the case , so you see this error.

Remember that there is nothing in the specification that says that all of your interfaces should be named with a capital of "I." is just a convention. Therefore, if your class implemented interfaces that did not comply with this convention, and if the specification allowed you to specify the base class and interfaces in any order, you cannot easily determine which identifier was the base class and which interfaces, Example:

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

it is called syntax

The following conventions must be followed in order for the compiler to compile code.

They could choose both forms, or just the opposite, but they did not. The reason is perhaps obvious: you can define many interfaces and inherit only one class.

Technically, it would be possible to allow them all at random, but that would make the code less readable.

+3
source

Just because the language is designed that way. The reason is probably because a class can have only one base class, but implement any number of interfaces.

+2
source

Since you can extend only one class and implement multiple interfaces, it will be easier to read at first. And perhaps the grammar itself is easier to write like this, just a pseudogram may be:

 class CLASSNAME:baseclass? interface* 

means an optional base class followed by many interfaces, writing one grammar that allows only one class, confused somewhere, will be difficult without any reason.

+2
source

You can inherit only one base class, but many interfaces. Therefore, if more than one type is specified in the list, you know that the first is a class, the rest are interfaces. This works regardless of class and interface naming conventions.

+1
source

The order has a clear view, the base class can implement the interface elements for the derived class, so the compiler must know about them in advance

+1
source

Source: https://habr.com/ru/post/885971/


All Articles