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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...