How to initialize a struct in C# [duplicate]

2020-02-12 07:38发布

问题:

I have some code to initialize a struct in C#:

namespace Practice
{
    public struct Point
    {
        public int _x;
        public int _y;

        public int X
        {
            get { return _x; }
            set { _x = value; }
        }

        public int Y
        {
            get { return _y; }
            set { _y = value; }
        }

        public Point(int x, int y)
        {
            _x = x;
            _y = y;
        }    
    }    

    class Practice
    {
        public static void Main()
        {
            Point p1;
            p1.X = 1;
            p1.Y = 2;
        }
    }
}

The above code gives a compiler error:

error CS0165: Use of unassigned local variable 'p1'

Why is this error being thrown?

回答1:

You can't use a property in a struct until it knows all the fields have been filled in.

For example, in your case this should compile:

Point p1;
p1._x = 1;
p1._y = 2;
int x = p1.X; // This is okay, now the fields have been assigned

Note how you don't have to explicitly call a constructor here... although in well-encapsulated structs you almost always would have to. The only reason you can get away with this is because your fields are public. Ick.

However, I would strongly advise you not to use a mutable struct anyway. If you really want a struct, make it immutable and pass the values into the constructor:

public struct Point
{
    private readonly int x;
    public int X { get { return x; } }

    private readonly int y;
    public int Y { get { return y; } }

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

...

Point p1 = new Point(1, 2);


回答2:

You have to initialize it with Point p1 = new Point();



回答3:

You need to create a Point first and assign it to p1:

public static void Main()
{
  Point p1 = new Point();
  p1.X = 1;
  p1.Y = 2;
}

By the way, you can have a constructor on your struct - could make things easier:

//in Point.cs
public point (int x, int y)
{
   _x = x;
   _y = y;
}

//in program.cs
public static void Main()
{
  Point p1 = new Point(1, 2);
}

This also allows you to avoid having setters on a struct (keeping it immutable).



回答4:

The statment: "Point p1;" requires a default constructor. The default constructor will not be automatically prodived because of the public Point(int x, int y) . You need to provide a default constructor: public Point() { ... }