error CS1501: No overload for method 'Base' takes '0' arguments

One situation that people may get into where the compiler error message may be confusing to them is when the default constructor (ctor) doesn't compile for a class.  Since Whidbey has a bit of a confusing error message for this case (we have a bug for Orcas for it), I wanted to post it on the blog just so someone trying to do a search might hit upon this page.

class Base { public Base(string foo) { } }

class Derived : Base

{

}

Program.cs(3,7): error CS1501: No overload for method 'Base' takes '0' arguments

Program.cs(1,21): (Related location)

As many of you know, in the absence of any specified ctor, the default no-args one is created, so the above source is logically equivalent to:

class Base { public Base(string foo) { } }

class Derived : Base

{

    Derived() : base() { } // implicit by the compiler

}

Once you realize that, the error becomes more clear - that call to base() is trying to call the no-args ctor of the Base class, which does not exist.

The fix depends on your situation, but you'll most likely either add a new no-args ctor to Base or add a non-default ctor to Derived, which I do here:

class Base { public Base(string foo) { } }

class Derived : Base

{

    Derived(string foo) : base(foo) { }

}

Comments