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