构造器将使其更具安排性和可读性:
struct a
{
int x;
int y;
byte[] z;
public a(int xv, int yv, byte[] zv)
{
x = xv;
y = yv;
z = zv;
}
}
public void Initialize()
{
var b = new a[] {new a(0,0,new byte[] { 0,0,0}),
new a(1,1,new byte[] { 1,1,2})};
}
根据您的评论,另一种方式
1、如果将结构字段的访问修饰符声明为public,则
将能够使用
object initializer and not
with constructor
(构造函数是一种方法)。
2、可以使用静态类并立即调用该对象
3、品牌
b
global和public(var是唯一的本地关键字),以便调用它
从外部(我会使用一个更具描述性的名称
b
).
完整示例:
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("y value of index 1 is: {0}", General.b[1].y);
Console.ReadLine();
}
}
public static class General
{
public static a[] b = new a[] { new a() { x = 0, y = 0, z = new byte[] { 0, 0, 0 }},
new a() { x = 1, y = 1, z = new byte[] { 1, 1, 1 }}
};
public struct a
{
public int x;
public int y;
public byte[] z;
}
}