Compiler Error CS0842
Automatically implemented properties cannot be used inside a type marked with StructLayout(LayoutKind.Explicit).
Automatically implemented properties have their backing fields provided by the compiler and the field is not accessible to source code. Therefore, they are not compatible with LayoutKind.Explicit.
- Make the property a regular property in which you provide the accessor bodies.
The following example generates CS0842:
// cs0842.cs
using System;
using System.Runtime.InteropServices;
namespace TestNamespace
{
[StructLayout(LayoutKind.Explicit)]
struct Str
{
public int Num // CS0842
{
get;
set;
}
static int Main()
{
return 1;
}
}
}
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.
.NET feedback
.NET is an open source project. Select a link to provide feedback: