Compiler Error CS0188

The 'this' object cannot be used before all of its fields are assigned to

All fields in a struct have to be assigned by a constructor before the constructor can call a method in the struct.

If you see this error when trying to initialize a property in a struct constructor, the solution is to change the constructor parameter to specify the backing field instead of the property itself. Auto-implemented properties should be avoided in structs because they have no backing field and therefore cannot be initialized in any way from the constructor.

For more information, see Using Structs (C# Programming Guide).

Example

The following sample generates CS0188:

// CS0188.cs
// compile with: /t:library
namespace MyNamespace
{
    class MyClass
    {
        struct S
        {
            public int a;

            void MyMethod()
            {
            }

            S(int i)
            {
                // a = i;
                MyMethod();  // CS0188
            }
        }
        public static void Main()
        { }

    }
}

See Also

Reference

Structs (C# Programming Guide)

Auto-Implemented Properties (C# Programming Guide)