C# Error CS0526 – Interfaces cannot contain instance constructors

C# Compiler Error

CS0526 – Interfaces cannot contain instance constructors

Reason for the Error

You’ll get this error in your C# code when you define a constructor for interfaces.

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

using System;
namespace DeveloperPublishNamespace
{
    public interface IDeveloperPublish
    {
        public IDeveloperPublish() 
        {
        }
    }

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

You’ll receive the error code CS0526 because you have defined the constructor inside the interface IDeveloperPublish. Constructor is usually a kind of method that has the same name as the class/type without any return type.

Error CS0526 Interfaces cannot contain instance constructors DeveloperPublish C:\Users\SenthilBalu\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 6 Active

C# Error CS0526 - Interfaces cannot contain instance constructors

Solution

In C#, the interface cannot contain constructors. It can contain only methods and properties.

To fix this error in your C# code, you will need to remove the constructor that you have defined.

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