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

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