[Solved] C# Error: Class does not contain a constructor that takes arguments
The C# compiler error CS1729: 'ClassName' does not contain a constructor that takes N arguments occurs when an object is created with a set of arguments that does not match any accessible constructor in the class.
To fix the error, make the arguments in the new expression match an existing constructor, add a constructor with the required parameter list, or create the object without arguments when a parameterless constructor is available.
Why C# Reports CS1729 for a Constructor Call
When C# compiles an expression such as new Book("C# Tutorial", "TutorialKart"), it searches the Book class for an accessible constructor whose parameters can accept two string arguments in that order.
If no matching constructor exists, the compiler cannot create the object and reports error CS1729. A constructor match depends on more than the number of arguments. The parameter types, order, accessibility, and optional parameters also affect whether a constructor can be called.
C# CS1729 Example with No Matching Book Constructor
The following program declares a Book class without an explicit constructor and then attempts to create a Book object by passing two arguments.
Program.cs
using System;
namespace CSharpExamples {
class Book{
public string author;
public string title;
public void printBookDetails(){
Console.WriteLine("\n---Book Details---\n==================");
Console.WriteLine("Title : "+title);
Console.WriteLine("Author : "+author);
}
}
class Program {
static void Main(string[] args) {
Book book1 = new Book("C# Tutorial", "TutorialKart");
book1.printBookDetails();
}
}
}
Output
Program.cs(16,30): error CS1729: 'Book' does not contain a constructor that takes 2 arguments [D:\workspace\csharp\HelloWorld\HelloWorld.csproj]
Program.cs(6,23): warning CS0649: Field 'Book.title' is never assigned to, and will always have its default value null [D:\workspace\csharp\HelloWorld\HelloWorld.csproj]
Program.cs(5,23): warning CS0649: Field 'Book.author' is never assigned to, and will always have its default value null [D:\workspace\csharp\HelloWorld\HelloWorld.csproj]
The build failed. Please fix the build errors and run again.
Because the class does not declare a constructor, C# provides an implicit parameterless constructor equivalent to Book(). That constructor accepts zero arguments, so it cannot be used for a call that supplies two strings.
Fix CS1729 by Adding a Matching C# Constructor
One solution is to add a constructor to Book that accepts the same number and types of arguments used in the object creation expression.
using System;
namespace CSharpExamples {
class Book{
public string author;
public string title;
public Book(string Title, string Author){
title = Title;
author = Author;
}
public void printBookDetails(){
Console.WriteLine("\n---Book Details---\n==================");
Console.WriteLine("Title : "+title);
Console.WriteLine("Author : "+author);
}
}
class Program {
static void Main(string[] args) {
Book book1 = new Book("C# Tutorial", "TutorialKart");
book1.printBookDetails();
}
}
}
Output
---Book Details---
==================
Title : C# Tutorial
Author : TutorialKart
The constructor Book(string Title, string Author) now matches the two string arguments supplied to new Book(...). It also assigns those values to the object’s fields.
Fix CS1729 by Calling the Parameterless Constructor
If the class does not need constructor arguments, create the object without passing values and assign its accessible members afterward.
Book book1 = new Book();
book1.title = "C# Tutorial";
book1.author = "TutorialKart";
book1.printBookDetails();
This works only when a parameterless constructor is available. If the class declares any constructor with parameters, C# no longer adds the implicit parameterless constructor automatically.
Add Both Parameterless and Parameterized Constructors
A class can support both styles of object creation by declaring more than one constructor. This is called constructor overloading.
class Book
{
public string title;
public string author;
public Book()
{
title = "Untitled";
author = "Unknown";
}
public Book(string title, string author)
{
this.title = title;
this.author = author;
}
}
With these constructors, both new Book() and new Book("C# Tutorial", "TutorialKart") are valid.
Check Constructor Argument Types and Order
Error CS1729 can occur even when the number of arguments appears correct. The argument types and their order must also match an accessible constructor.
class Product
{
public Product(string name, decimal price)
{
}
}
Product first = new Product("Keyboard", 49.99m); // Valid
Product second = new Product(49.99m, "Keyboard"); // CS1729
The second call passes the same two values in the wrong order. No constructor accepts (decimal, string), so the compiler cannot resolve the call.
Check Whether the Matching Constructor Is Accessible
A constructor may have the correct parameters but still be unavailable because of its access modifier. For example, a private constructor cannot normally be called from another class.
class Report
{
private Report(string name)
{
}
}
class Program
{
static void Main()
{
Report report = new Report("Annual");
}
}
In this situation, either change the constructor’s access modifier when direct construction should be allowed or use the factory method or other creation mechanism provided by the class.
Use Named Arguments to Avoid Constructor Order Mistakes
Named arguments can make constructor calls clearer and can prevent mistakes when several parameters have the same type.
Book book1 = new Book(
Title: "C# Tutorial",
Author: "TutorialKart"
);
The argument names must match the constructor parameter names. Named arguments improve readability, but they do not allow a call to bypass missing or incompatible constructor parameters.
CS1729 Constructor Error Troubleshooting Checklist
- Count the arguments passed after
new ClassName(...). - Find every constructor declared in the target class.
- Compare the argument types with the constructor parameter types.
- Verify that arguments appear in the correct order.
- Check whether the intended constructor is
publicor otherwise accessible from the calling code. - Remember that declaring a parameterized constructor removes the automatically supplied parameterless constructor.
- Check whether optional parameters or named arguments are being used correctly.
- Confirm that the code is instantiating the intended class when multiple classes have similar names or namespaces.
C# CS1729 Constructor Error FAQs
What does CS1729 mean in C#?
CS1729 means the compiler cannot find an accessible constructor whose parameter list matches the arguments supplied in the object creation expression.
Why does new ClassName() fail after I add another constructor?
Once a class declares an instance constructor, C# does not automatically generate a parameterless constructor. Add an explicit ClassName() constructor if objects should still be created without arguments.
Can optional constructor parameters prevent CS1729?
Yes, when omitted arguments correspond to parameters that have default values. The supplied arguments must still match the remaining parameter types and ordering rules.
Can a private constructor cause a constructor error?
Yes. A constructor with matching parameters cannot be called from code that does not have access to it. Private constructors are commonly used with factory methods, singleton patterns, and classes that should not be instantiated directly.
Resolve the C# Constructor Argument Mismatch
To resolve Class does not contain a constructor that takes arguments, compare the new expression with the constructors declared by the class. Add a matching constructor, correct the argument types or order, use an accessible overload, or call a parameterless constructor when that matches the intended design.
In this C# Tutorial, we learned how to diagnose and fix the C# error stating that a class does not contain a constructor that takes the supplied arguments.
TutorialKart.com