C# Error CS0515 – ‘function’ : access modifiers are not allowed on static constructors

C# Compiler Error

CS0515 – ‘function’ : access modifiers are not allowed on static constructors

Reason for the Error

You’ll get this error in your C# code when you have specified the access modifiers for the static constructors.

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

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

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

You’ll receive the error code CS0515 because the class BaseClass has a static constructor and has the public access modifier specified for it.

Error CS0515 ‘BaseClass.BaseClass()’: access modifiers are not allowed on static constructors DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

C# Error CS0515 – 'function' : access modifiers are not allowed on static constructors

Solution

In C#, you cannot have the access modifiers for the static constructor. To fix the error code CS0515, you will need to remove the access modifier for the static constructor.

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