Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0842

Visual Studio 2008

Automatically implemented properties cannot be used inside a type marked with StructLayout(LayoutKind.Explicit).

Auto-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

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

Community Additions

ADD
Show:
© 2014 Microsoft