相关疑难解决方法(0)

如何在C#中初始化结构

我有一些代码来初始化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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码给出了编译器错误: …

c# struct initialization

6
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×1

initialization ×1

struct ×1