How to Resolve Error "CS0143: The type 'type' has no constructors defined" in C#
The Compiler Error CS0143 is a syntax and type-safety error. The message reads: "The type 'MyType' has no constructors defined".
While standard classes almost always have at least a default constructor, this error most frequently appears when working with Delegates. In modern C#, attempting to instantiate a class with the wrong arguments usually results in error CS1729. However, attempting to instantiate a Delegate with invalid arguments (like a number or a string instead of a method) specifically triggers CS0143.
This guide explains why this happens and how to properly instantiate objects and delegates.
Understanding Constructors and Delegates
- Classes: When you create a class using
new, you call a Constructor. If you don't define one, C# creates a hidden, empty one:public ClassName() { }. - Delegates: A delegate is a pointer to a method. Internally, a delegate has a constructor that accepts a Method Pointer (and a target object). It does not accept arbitrary data like integers or strings.
CS0143 typically means you tried to pass data (like 10 or "Hello") into a type (like a Delegate) that has no logic to accept data in its constructor.
Scenario 1: Invalid Delegate Instantiation (Most Common)
This is the primary cause of this specific error code in modern .NET. You define a delegate that expects to hold a reference to a function, but you try to initialize it with a value.
Example of error: you might think passing arguments to the delegate constructor passes them to the method later. This is incorrect.
public delegate void MessageHandler(string message);
public class Program
{
static void Main()
{
// ⛔️ Error CS0143: The type 'MessageHandler' has no constructors defined
// You cannot pass "Hello World" to the delegate constructor.
// The delegate constructor expects a METHOD name, not string data.
MessageHandler handler = new MessageHandler("Hello World");
}
}
Scenario 2: Class Argument Mismatch
While mostly covered by error CS1729 in newer compilers, CS0143 can still occur in specific contexts where built-in types or COM objects are initialized with arguments they do not support.
Example of error: trying to pass arguments to a class that only has the default (parameterless) constructor.
public class User
{
public string Name;
// No constructor defined -> Default constructor User() is created.
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0143 (or CS1729): The type 'User' has no constructors defined
// (that accept a string).
var u = new User("Alice");
}
}
Solution 1: Pass the Correct Method to Delegate
To fix the delegate error, you must pass a method name (without parentheses) to the delegate constructor, or use a lambda expression.
Correct Usage:
public delegate void MessageHandler(string message);
public class Program
{
// A method that matches the delegate signature
public static void PrintRed(string msg)
{
System.Console.WriteLine($"Red: {msg}");
}
static void Main()
{
// ✅ Correct: Pass the Method Name to the constructor
MessageHandler handler = new MessageHandler(PrintRed);
// ✅ Correct: Modern shorthand (Method Group Conversion)
MessageHandler handler2 = PrintRed;
// Invoke the delegate later with the data
handler("Hello World");
}
}
A delegate constructor accepts a Target Method. It does not accept the Arguments for that method. You provide the arguments later when you invoke the delegate (e.g., handler("Data")).
Solution 2: Use Object Initializers for Classes
If you got this error while trying to populate a class's data during creation, and the class does not have a custom constructor, use Object Initializer syntax.
Solution: instead of passing data in parentheses (), assign properties inside curly braces {}.
public class User
{
public string Name { get; set; }
public int Id { get; set; }
}
public class Program
{
static void Main()
{
// ⛔️ Incorrect
// var u = new User("Alice", 101);
// ✅ Correct: Use Object Initializer syntax
var u = new User
{
Name = "Alice",
Id = 101
};
}
}
Alternatively, explicitly define a constructor in your class if you want to enforce arguments:
public class User
{
public User(string name) { Name = name; }
public string Name { get; set; }
}
// Now 'new User("Alice")' is valid.
Conclusion
CS0143 indicates you are misusing the new keyword.
- Check Delegates: If instantiating a delegate, ensure you are passing a Method Name, not data values.
- Check Classes: If instantiating a class, ensure a constructor exists for the arguments you are passing.
- Use Initializers: If the class has no constructor, use
{ Property = Value }syntax instead of(Value).