HomeCSharpC# Error CS0514 – ‘constructor’ : static constructor cannot have an explicit ‘this’ or ‘base’ constructor call

C# Error CS0514 – ‘constructor’ : static constructor cannot have an explicit ‘this’ or ‘base’ constructor call

C# Compiler Error

CS0514 – ‘constructor’ : static constructor cannot have an explicit ‘this’ or ‘base’ constructor call

Reason for the Error

You’ll get this error in your C# code when you attempt to call this operator inside the static constructor.

For example, let’s try to compile the below C# code snippet.

using System;
namespace DeveloperPublishNamespace
{
    public class BaseClass
    {
        static BaseClass() : this()
        {

        }
    }
    class Program
    {    
        static void Main(string[] args)
        {
            Console.WriteLine("No Error");
        }
    }
}

You’ll receive the error code CS0514 because the class BaseClass has a static constructor ans we are calling this inside it.

Error CS0514 ‘BaseClass’: static constructor cannot have an explicit ‘this’ or ‘base’ constructor call DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

C# Error CS0514 – 'constructor' : static constructor cannot have an explicit 'this' or 'base' constructor call

Solution

In C#, you cannot call “this” in the static constructor because the static constructor is created automatically before creating the instance of the class. To fix this error, you will need to avoid doing this.

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...