Skip to main content

How to Resolve Error "CS0573: 'Field': cannot have instance field initializers in structs" in C#

The Compiler Error CS0573 is a restriction related to Structs in older versions of C#. The message reads: " 'Field': cannot have instance field initializers in structs".

In C#, a struct is a value type. Historically (prior to C# 10), structs were required to perform all initialization inside a constructor to ensure performance predictability. This meant you could not assign a default value directly where the field was declared (e.g., public int x = 10;).

While C# 10 and C# 11 have relaxed these rules significantly, you will still encounter this error if you are using an older language version (like C# 9.0 in .NET 5 or .NET Framework 4.8) or if you have specific layout modifiers.

This guide explains the restriction and how to initialize struct fields correctly.

Understanding the Struct Limitation​

Unlike classes, structs are value types that are often created without calling a constructor (e.g., creating an array new MyStruct[10] creates 10 structs initialized to zero/null).

If C# allowed inline initializers (int x = 5) in older versions, the runtime would have to execute that initialization logic every time a struct was created, which contradicts the "zero-overhead" default behavior of value types. Therefore, the compiler historically enforced that fields must either start at 0/null or be set explicitly in a constructor you define.

Scenario: Inline Initialization in Older C#​

This error commonly occurs when a developer treats a struct exactly like a class.

Example of error:

public struct Point
{
// ⛔️ Error CS0573: 'Point.X': cannot have instance field initializers in structs
public int X = 10;

// ⛔️ Error CS0573
public int Y = 20;
}

Solution 1: Use a Constructor (The Standard Fix)​

If you are on an older version of .NET (Framework, Core 3.1, or .NET 5), you must move the initialization logic into a parameterized constructor.

Solution:

public struct Point
{
public int X;
public int Y;

// ✅ Correct: Initialize fields inside the constructor.
public Point(int x, int y)
{
X = x;
Y = y;
}
}

// Usage:
// var p = new Point(10, 20);
note

Default Constructor: Prior to C# 10, you cannot define a parameterless constructor public Point() { ... } in a struct. You must rely on the default (zeroed) state or use a factory method.

Solution 2: Upgrade to C# 10+​

If you are using .NET 6 or newer (which defaults to C# 10), the language rules have changed. Inline field initializers for structs are allowed.

If you are seeing this error in a modern project, check your .csproj file. You might be inadvertently forcing an old language version.

How to Check Language Version​

Open your .csproj file and check for <LangVersion>.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<!-- ⛔️ Problem: This forces C# 9 rules, where field init is illegal -->
<LangVersion>9.0</LangVersion>
</PropertyGroup>
</Project>

Solution​

Remove the LangVersion tag (to use the SDK default) or set it to 10.0 or higher.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!-- ✅ Correct: C# 10+ supports struct field initializers -->
<LangVersion>10.0</LangVersion>
</PropertyGroup>
</Project>

After upgrading:

public struct Point
{
// ✅ Valid in C# 10+
public int X = 10;
public int Y = 20;
}

Conclusion​

CS0573 is primarily a versioning restriction.

  1. Check your Version: Are you using .NET Framework or an old C# version?
  2. The Code Fix: Move assignments from the field declaration (int x = 10;) to a constructor.
  3. The Project Fix: If on .NET 6+, upgrade your language version to C# 10 to allow this syntax.