Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 997
  • Last Modified:

The name of the base class must come before any interface. Why?

Enclosed code would give you the "Class must come first before interface" error. I understand it is a rule, but can anybody tell me what the reason behind it?
public interface ITest
    {
        int ChildCount { get; set; }
    }
 
public class Test
    {
    }  
 
public class OrderPool : ITest, Test
    {
        public int ChildCount
        {
            get;
            set;
        }     
    }

Open in new window

0
redflair
Asked:
redflair
2 Solutions
 
rambovnCommented:
it is something like the params keyword.
0
 
philipjonathanCommented:
I'm not sure why. But on the practical side, it's easier to read that way, because a class can implement multiple interface, but can only inherit directly from 1 base class. So by 1 glance, you would already know if a class inherits from a base class. If the base class can come in the middle of interface names, it might be a little harder to read, especially if it implements many interfaces.
0
 
ppittleCommented:
Philip is on the rihgt path.  C# implements a restrictive form of polymorphism where a class can only inherit from one base class, but can implement multiple interfaces, as opposed to a language like C++ where you can inherit from multiple base classes.  The rational behind this, is you open several odd fringe cases and reap little benefits.  For example, lets say you create class ChildClass which is to inherit from BaseClass1 and BaseClass2.  Both BaseClass1 and BaseClass2 provide a custom ToString method.  So when you write "ChildClass.ToString()"  the compiler doesn't know whether to call BaseClass1.ToString() or BaseClass2.ToString(), so you would end up with some odd behavior.

To simplify the class declaration, a baseclass must be the first object folowing the semicolon.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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