error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Customer.Customer(string)'
means a call leaves out a parameter that has no default value. With constructors it has two
common sources: new Customer() after the class gained a constructor with a
parameter, and a derived class whose base class has no parameterless constructor. Pass the
argument, and give the derived class a constructor that calls : base(name).
Writing any constructor removes the free, empty one C# would otherwise generate, which is why code that compiled yesterday breaks after a constructor is added. Read the message from the end: the name after "of" is the constructor being called, and the first quoted name is the parameter it is missing.
The error
...\Program.cs(15,7): error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Customer.Customer(string)' [...\FixLab.csproj]
...\Program.cs(1,20): error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Customer.Customer(string)' [...\FixLab.csproj]
Build FAILED.
The folder part of the paths was shortened to ...; nothing else was changed.
Why it happens
var customer = new Customer();
customer.Name = "Maria Garcia";
Console.WriteLine(customer.Name);
class Customer
{
public Customer(string name)
{
Name = name;
}
public string Name { get; set; }
}
class VipCustomer : Customer
{
public int Level { get; set; }
}
Line 1, column 20: new Customer() passes nothing, but the only
constructor needs name. A class with no constructor gets an empty one from the
compiler; as soon as you write one yourself, that free one is gone.
Line 15, column 7: the class name VipCustomer. It declares no
constructor, so the compiler generates an empty one, and that one must call a base
constructor with no arguments. Customer has none, so the generated
constructor cannot be built and the error points at the class itself, naming the base
constructor's parameter.
The fix
var customer = new Customer("Maria Garcia");
Console.WriteLine(customer.Name);
var vip = new VipCustomer("David Chen", 2);
Console.WriteLine($"{vip.Name}, level {vip.Level}");
class Customer
{
public Customer(string name)
{
Name = name;
}
public string Name { get; set; }
}
class VipCustomer : Customer
{
public VipCustomer(string name, int level) : base(name)
{
Level = level;
}
public int Level { get; set; }
}
The call passes the name, and VipCustomer has its own constructor that hands
name to Customer with : base(name). The project built
with warnings treated as errors and printed Maria Garcia and
David Chen, level 2.
Adding an empty public Customer() { } back would also compile, but then a
customer can exist without a name, which is what the constructor was written to prevent.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0, nullable and
implicit usings on, no packages) with the program above. dotnet build
--no-incremental failed with the two CS7036 errors; the fixed version passed
dotnet build --no-incremental -p:TreatWarningsAsErrors=true and ran.
Frequently asked
- How do I fix there is no argument given that corresponds to the required parameter?
- Pass a value for the named parameter in the call, for example new Customer("Maria Garcia"). If the error points at a derived class, give it a constructor that calls the base constructor with : base(name).
- Why did adding a constructor break new Customer()?
- C# generates an empty constructor only for a class that declares none. Once you write a constructor with parameters, the empty one is gone, and every new Customer() without arguments stops compiling.
- How do I call a base class constructor in C#?
- Add : base(arguments) after the derived constructor's parameter list, for example public VipCustomer(string name, int level) : base(name), as in the fixed program, which built and printed David Chen, level 2.
More decoded compiler errors are in the Fixes category.