ramki.v

Hi,

Maximum number of classes can be inherited to a class in C#


Ramki.V


Re: Visual C# General Maximum number of classes can be inherited to a class in C#?

Alberto Poblacion

Let's see if I can interpret your question correctly:

Do you mean "from how many classes can a class inherit"

In this case, the answer is "One", or in other words, multiple class inheritance is not supported in C# (but you can partially approach it by using interfaces).

If you mean "how many classes can inherit from a class", then the answer is "no limit".

If you mean "in a hierarchy of parent and child classes, how many levels of children can you have", then the answer is that there is no set limit, but you will want to keep to a moderate number of levels if you want your hierarchy of classes to be practical.





Re: Visual C# General Maximum number of classes can be inherited to a class in C#?

ramki.v

Thanks for your reply,

I got few more doubts after reading your answer.

In a hierarchy of parent and child classes, How many levels of children can we have (any practical figure limit)

and one more doubt, from how many classes can a class interface ( i need a some practical figure)





Re: Visual C# General Maximum number of classes can be inherited to a class in C#?

Alberto Poblacion

I've not seen any documented limits for either hierarchy levels or number of interfaces, so out of curiosity I wrote a small test program which generates a source file with lines similar to the following:

class Class963 : Class962 { public override string Test() { return "Class963"; } }

I iterated this over 1000 lines (meaning a hierarchy of inherited classes 1000 levels deep), and it compiled without problem.

However, trying to create an instance of Class1000 and invoking the function Test() resulted in a Stack Overflow. The maximum numbers of levels I was able to invoke was around 250. So I imagine that answers your question: anything over approximately 250 levels of children will compile but not be usable because calling its functions will throw a Stack Overflow.

As to the number of interfaces, I leave it as an exercise for you to write a similar test and see how far you can go without crashing something :-)