Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Compiler Warning (level 1) CS1060
Collapse the table of content
Expand the table of content

Compiler Warning (level 1) CS1060

Use of possibly unassigned field 'name'. Struct instance variables are initially unassigned if struct is unassigned.

Struct members are initialized to their default value if you do not explicitly initialize them. The default value for class types (and other reference types) is null. If the class is not initialized before any attempt to access it, a NullReferenceException will be thrown at runtime. The compiler cannot determine definitively whether the class member will be initialized or not, and so CS1060 is a warning and not an error.

To correct this error

  • Provide a constructor for the struct that initializes all its members.

The following code generates CS1060 because the class type U is a member of the struct S but is never initialized.

// cs1060.cs
namespace CS1060
{    
    public class U
    {
        public int i;
    }

    public struct S
    {
        public U u;
        // Add constructor to correct the error.
        //public S(int val)
        //{
        //    u = new U() { i = val };
        //}
    }
    public class Test
    {
        static void Main()
        {
            S s;
            s.u.i = 5;  // CS1060

            //Try these lines instead, and uncomment the constructor in S
            // S s = new S(0);
            // s.u.i = 5;
                 
        }
    }  
}
Show:
© 2015 Microsoft