0

I was refreshing my limited knowledge of inheritance in c# 11 because I need it for a project. However I got unexpected results when calling the constructor of an inherited class. Any call to the inherited constructor always resulted in a call to base constructor first. I was under the impression that the normal way to call the base constructor before the inherited constructor is to do the following:

internal DerivedClass()
: base()
{
   // Do stuff here. Behaves as expected
}

However the same functionality is achieved without the : base():

class Program
{
    static void Main(string[] args)
    {
        new BaseClass(); // Prints "BaseClass". Behaves as expected
        new DerivedClass(); // Prints "BaseClass"\n"DerivedClass". Expected behavior: "DerivedClass"
        new DerivedClass("test"); // Prints "BaseClass"\n"test". Expected behavior: "test"
    }
}

class BaseClass
{
    internal BaseClass()
    {
        Console.WriteLine("BaseClass");
    }
}

class DerivedClass : BaseClass
{
    internal DerivedClass()
    {
        Console.WriteLine("DerivedClass");
    }

    internal DerivedClass(string test) // Different parameters than BaseClass()
    {
        Console.WriteLine(test);
    }
}

So how do I call the constructor of a derived class without constructor chaining? I thank anyone answering in advance.

What I did so far:

  • Confirming the described execution flow with the dotnet debugger
  • Searching the web (no relevant results)
  • Searching StackOverflow (no relevant results)
  • Reading the Microsoft tutorial on inheritance (not useful for this problem and the publication example had ambiguity errors out of the box (it wasn't relevant to this problem anyway))
  • I am currently in Italy and OpenAI blocks ProtonVPN so I currently have no access to ChatGPT
DogPerson
  • 11
  • 1
  • 3
  • your expected behavior doesnt have relation with your code. I think the error is in the basic understanding of OOP – Leandro Bardelli Apr 16 '23 at 21:52
  • @LeandroBardelli Could you please elaborate what you're suggesting? What I mean with expected behavior is what I would expect the called constructor to print to the command line (this is a simplified example after all) based on the code, i.e. with the expression `new DerivedClass();` I would expect the constructor of DerivedClass to be called and which then prints "DerivedClass" to the console with `Console.WriteLine("DerivedClass");`. What happened instead is that this expression first called the constructor of BaseClass and printed "BaseClass" before calling the BaseClass` constructor. – DogPerson Apr 16 '23 at 22:08
  • @DogPerson that's how the OOP works, the base class constructor is always called – Ehsan Sajjad Apr 16 '23 at 22:17
  • You can actually do this in VB.NET: do stuff in the derived constructor before the base one. But the base must always be called at some point. – Charlieface Apr 16 '23 at 22:18
  • [C# Constructors](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors). Note the steps that it lays out and in what order. Note that it doesn't indicate that any of them are optional. Note that step 4 is running the base class constructors. – Damien_The_Unbeliever Apr 17 '23 at 06:00

0 Answers0