初始化一个结构体数组
C#,Visual studio 2010
我想要声明一个结构体数组并同时初始化它,但无法正确执行 我如何写入默认初始化由结构组成的数组?
以下内容不会通过编译器,但会显示我想要存档的内容的想法
private struct PrgStrTrans_t
{
public int id;
public string name;
public string fname;
}
private PrgStrTrans_t[] PrgStrTrans = { {1, "hello", "there"}, {2, "Fun", thisone"}}
是否可能?
Possible Duplicates:
How to initialize array of struct?
Initializing an Array of Structs in C#
C#, Visual studio 2010
I want declare an array of struct and initialize it on the same time but can not get it right
How do i write to default initialize an array consisting of structs ?
The following wont go through the compiler but show the idea of what I want to archive
private struct PrgStrTrans_t
{
public int id;
public string name;
public string fname;
}
private PrgStrTrans_t[] PrgStrTrans = { {1, "hello", "there"}, {2, "Fun", thisone"}}
Is it possible at all ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将构造函数添加到结构中,并将
new PrgStrTrans(...),
放在数组的每一行上。像这样:
Add a constructor to your struct, and put
new PrgStrTrans(...),
on each line of the array.Like this:
如果您创建一个构造函数会更好,这样可以避免键入属性名称。
It would be better if you made a constructor, that would avoid typing the property names.