Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Members of read-only field 'name' of type 'struct name' cannot be assigned with an object initializer because it is of a value type.
Read-only fields that are value types can only be assigned in a constructor.
To correct this error
Change the struct to a class type.
Initialize the struct with a constructor.
Example
The following code generates CS1917:
// cs1917.cs
class CS1917
{
public struct TestStruct
{
public int i;
}
public class C
{
public readonly TestStruct str = new TestStruct();
public static int Main()
{
C c = new C { str = { i = 1 } }; // CS1917
return 0;
}
}
}
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.