Compiler Error CS0573
'field declaration' : cannot have instance field initializers in structs
You can't initialize an instance field of a struct. Fields of value types will be initialized to their default values, and reference-type fields will be initialized to null
.
Note
Beginning with C# 10, you can initialize a struct's instance field or property at its declaration. For more information, see the Struct initialization and default values section of the Structure types article.
Example
The following sample generates CS0573:
// CS0573.cs
namespace x
{
public class clx
{
public static void Main()
{
}
}
public struct cly
{
clx a = new clx(); // CS0573
// clx a; // OK
int i = 7; // CS0573
// int i; // OK
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for