Edit

Share via


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.

To correct this error

  1. Make the property a regular property in which you provide the accessor bodies.

Example

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;  
        }  
    }  
}