This code builds fine.
Yes - why do you think this is not so?
What could be a possible reason that C # allows you to create objects of a derived class in baseClass.
Because there is no reason to ban it?
Can you come up with any specific reasons for this?
Static factory methods, for example?
// BaseClass gets to decide which concrete class to return public static BaseClass GetInstance() { return new DerivedClass(); }
This is a really fairly common pattern. We often use it in Noda Time , where CalendarSystem is an open abstract class, but all concrete derived classes are internal.
Of course, crazy to have the exact example you specified - with an instance field that initializes itself by instantiating the derived class because it would explode the stack due to recursion - but this is not a question of being a derived class. You will get the same by initializing the same class:
class Bang {
source share