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

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