1

I wonder if there is any reason to call the base constructor when inheriting from object.

class Foo
{
    public Foo()
        : base() // calling the Object constructor 
    {
        // Foo construct here
    }
}

Is there a way to inject code into the base constructor? Object doesn't seem to have multiple constructors.

1 Answer 1

6

There is no need to explicitly call the base constructor in this situation. The base constructor is automatically called if it has no parameters. So the code is equivalent to just doing:

class Foo
{
    public Foo()
    {
        // Foo construct here
    }
}

: base is only needed if the base class needs parameters passed to its constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.