Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0516 – Constructor ‘constructor’ can not call itself

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");
        }
    }
}

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

C# Error CS0516 – Constructor 'constructor' can not call itself

Solution

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