C# Compiler Error
CS0516 – Constructor ‘constructor’ can not call itself
Reason for the Error
You’ll get this error in your C# code when you try to call the constructor recursively.
For example, let’s try to compile the below C# code snippet.
using System; namespace DeveloperPublishNamespace { public class BaseClass { public BaseClass() : this() { } } class Program { static void Main(string[] args) { Console.WriteLine("No Error"); } } }
C#
x
18
1
using System;
2
namespace DeveloperPublishNamespace
3
{
4
public class BaseClass
5
{
6
public BaseClass() : this()
7
{
8
9
}
10
}
11
class Program
12
{
13
static void Main(string[] args)
14
{
15
Console.WriteLine("No Error");
16
}
17
}
18
}
You’ll receive the error code CS0516 because the class BaseClass has a constructor and we are attempting to call the constructor itself using the this() operator.
Severity Code Description Project File Line Suppression State
Error CS0516 Constructor ‘BaseClass.BaseClass()’ cannot call itself DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

Solution
To fix this error, avoid calling the constructor recursively. Remove the this() from the above code snippet to fix this error.