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