C# Error CS0568 – Structs cannot contain explicit parameterless constructors

C# Compiler Error

CS0568 – Structs cannot contain explicit parameterless constructors

Reason for the Error

You will get this error in your C# code when you have a struct with an explicit parameterless constructor. This error doesnot appear in the new version of C# (C#10) as a struct type in C# 10 can contain explicit parameterless constructor.

For example, let’s compile the below C# program

using System;

namespace DeveloperPublishConsoleCore
{
    public struct Employee
    {
        public int id;
        public Employee() { }  
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0568 because the struct Employee contains an explicit parameterless constructor.

CS0568 – Structs cannot contain explicit parameterless constructors

C# Error CS0568 – Structs cannot contain explicit parameterless constructors

You may also get the below error depending on the version of C# that you are on.

Error CS0171 Field ‘Employee.id’ must be fully assigned before control is returned to the caller DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 8 Active

Error CS8400 Feature ‘parameterless struct constructors’ is not available in C# 8.0. Please use language version 10.0 or greater. DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 8 Active

C# Error CS0568 – Structs cannot contain explicit parameterless constructors

Solution

You can fix this error in your C# code by either having a constructor with at least one parameter or look at upgrading to latest version of C# (C# 10).

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