C#中结构体中包含结构体数组,怎么定义?

structa{intstart;intend;};structb{intid;a[]10=newa[10];};提示错误结构中不能有实例字段初始值设定项... struct a
{
int start;
int end;
};
struct b
{
int id;
a[]10=new a[10];
};
提示错误 结构中不能有实例字段初始值设定项
展开
 我来答
百度网友0f753cee
2012-04-28 · TA获得超过383个赞
知道小有建树答主
回答量:113
采纳率:0%
帮助的人:37.2万
展开全部
struct b
{
int id;
a[] suba;
};
程序调用时:采用 b ab; ab.suba=new a[10];的方式。
但是不推荐这样用,可以用类去代替struct ;
注:结构体内的变量最好加public 关键词。
即:struct b{public int id;public a[] suba;}

struct a
{
public int start;
public int end;
}
struct b
{
public int id;
public a[] suba;
}
static void Main(string[] args)
{
b newb;
newb.id = 1;
newb.suba = new a[10];
newb.suba[0].start = 1;
newb.suba[0].end = 2;
newb.suba[1].start = 3;
newb.suba[1].end = 4;
Console.WriteLine("{0},{1},{2},{3}", newb.suba[0].start, newb.suba[0].end,newb.suba[1].start,newb.suba[1].end);
Console.Read();
}
结果输出1,2,3,4
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xiangjuan314
2015-12-25 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:3588万
展开全部
C#结构体定义的情况:
C#结构体定义也可以象类一样可以单独定义.
class a{};
struct a{};

C#结构体定义也可以在名字前面加入控制访问符.
public struct student{};
internal struct student{};

如果结构体student没有publice或者internal的声明 类program就无法使用student结构定义 obj对象,如果结构体student的元素没有public的声明,对象obj就无法调用元素x
因为默认的结构体名和元素名是private类型
C#结构体定义之程序:
using System;
public struct student
{
public int x;
};
class program
{
public static void Main()
{
student obj=new student();
obj.x=100;
}
};

在结构体中也可以定义静态成员与类中一样,使用时必须用类名,或结构名来调用不属于实例,声明时直接定义.
C#结构体定义程序:
using System;
public struct student
{
public static int a = 10;
};
class exe
{
public static void Main()
{
Console.WriteLine( student.a = 100);
}
};


using System;
class base
{
public struct student
{
public static int a = 10;
};
}
class exe
{
public static void Main()
{
Console.WriteLine( base.student.a = 100);
}
};

在结构体中可以定义构造函数以初始化成员,但不可以重写默认无参构造函数和默认无参析构函数
C#结构体定义程序:
public struct student
{
public int x;
public int y;
public static int z;
public student(int a,int b,int c)
{
x=a;
y=b;
student.z=c;
}
};

在结构体中可以定义成员函数。
C#结构体定义程序:
public struct student
{
public void list()
{
Console.WriteLine("这是构造的函数");
}
};

结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象
C#结构体定义程序:
public struct student
{
public int x;
public int y;
public static int z;
public student(int a,int b,int c)
{
x=a;
y=b;
student.z=c;
}
};
class program
{
public static void Main()
{
student obj=new student(100,200,300);
student obj2;
obj2.x=100;
obj2.y=200;
student.z=300;
}
}

在使用类对象和函数使用时,使用的是引用传递,所以字段改变
在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变
C#结构体定义程序:
using System;
class class_wsy
{
public int x;
}
struct struct_wsy
{
public int x;
}
class program
{
public static void class_t(class_wsy obj)
{
obj.x = 90;
}
public static void struct_t(struct_wsy obj)
{
obj.x = 90;
}
public static void Main()
{
class_wsy obj_1 = new class_wsy();
struct_wsy obj_2 = new struct_wsy();
obj_1.x = 100;
obj_2.x = 100;
class_t(obj_1);
struct_t(obj_2);
Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x); Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);
Console.Read();
}
}

C#结构体定义程序运行结果为:
class_wsy obj_1.x=90
struct_wsy obj_2.x=100
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式