在c# 结构组成数组如何定义和初始化

在c中,typedef struct abc{char a,int b,float c}; const struct buff[]={{1,2,3};{4,5,6};{7,8,9}};这段在c#中如何实现?

第1个回答  推荐于2016-06-26
struct abc
{
char a;
int b;
float c;
};
class Program
{
readonly int[,] buff =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
}追问

你这样的话,我定义结构体abc干嘛呢,不是二位数组,是数组成员是结构体,因为结构体元素类型都不同

追答

using System;
struct abc
{
char a;
int b;
float c;
public abc(char a, int b, float c)
{
this.a = a;
this.b = b;
this.c = c;
}
};
class Program
{
readonly abc[] buff =
{
new abc('1', 2, 3),
new abc('4', 5, 6),
new abc('7', 8, 9),
};
}

追问

这样是可以的,谢谢,readonly和const是一样的吗

追答

readonly 关键字与 const 关键字的区别如下:
const 字段只能在该字段的声明中初始化。
readonly 字段可以在声明或构造函数中初始化。
因此,根据所使用的构造函数,readonly 字段可能具有不同的值。
另外,const 字段是编译时常数,而 readonly 字段可用于运行时常数。

本回答被提问者采纳
第2个回答  2012-12-28
int array[ , ]={{,1,2,3},{4,5,6},{7,8,9}}
相似回答