C# Error CS0522 – ‘constructor’ : structs cannot call base class constructors

C# Compiler Error

CS0522 – ‘constructor’ : structs cannot call base class constructors

Reason for the Error

You’ll get this error in your C# code when you attempt to call the base class constructor from the struct.

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

using System;
namespace DeveloperPublishNamespace
{
    public class BaseClass
    {
        public BaseClass(int i)
        {

        }
    }
    public struct samplestruct
    {
        public samplestruct(int i) : base(0)
        {

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

You’ll receive the error code CS0522 because the struct “samplestruct” has a constructor that is calling the base class constructor.

Error CS0522 ‘samplestruct’: structs cannot call base class constructors DeveloperPublish C:\Users\Senthil\source\repos\ConsoleApp4\ConsoleApp4\Program.cs 13 Active

C# Error CS0522 – 'constructor' : structs cannot call base class constructors

Solution

To fix this error in your C# code, you will need to remove the call to the base class constructor from the struct.

using System;
namespace DeveloperPublishNamespace
{
    public class BaseClass
    {
        public BaseClass(int i)
        {

        }
    }
    public struct samplestruct
    {
        public samplestruct(int i)
        {

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

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