注册 登录
编程论坛 C# 论坛

求大神帮忙,结构体中数组怎么实例化?或者能把我下面代码改对了就OK

dreamingost 发布于 2014-03-02 15:21, 805 次点击
public struct STONES
    {
        public int x;
        public int y;
    };
    public struct Lines
    {
        public STONES[] Point = new STONES[2];//错误
        public int[] items= new int[6];//错误
        public int value;
        public int color;
    };
5 回复
#2
wp2319572014-03-02 15:33
我刚试过了 在main中可以使用你的类似代码  
但是你在一个struct中去初始化另一个struct 这个貌似不可以
#3
dreamingost2014-03-02 15:36
回复 2楼 wp231957
我这边显示的错误是:结构中不能有实例字段初始值设定项。。。纠结死了。。
#4
wp2319572014-03-02 15:45
百度一下  “结构中不能有实例字段初始值设定项”  看看  好像是需要变通处理
#5
shangsharon2014-03-02 19:09
不能实例化就不去实例化啊.

public struct STONES
    {
        public int x;
        public int y;
    };
    public struct Lines
    {
        public STONES[] Point ;
        public int[] items;
        public int value;
        public int color;
    };
   
    public class Program{
    static void Main(){
    Lines line=new Lines();
    STONES stone=new STONES();
    stone.x=12;
    stone.y=21;
   
    line.Point=new STONES[]{stone,stone};
   
    System.Console.WriteLine(line.Point.Length);
    }
    }
#6
TonyDeng2014-03-02 22:47
改對的前提是你想幹什麽
1