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

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...