如何创建结构数组 [英] how to create structure array

查看:98
本文介绍了如何创建结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想收集所有显示分辨率的详细信息.


Hi,
I want to collect all the display resolution details in variable.


HI,
Yes I want to create a array of structures, to store multiple value of below details.
  public struct slotInfo
        {
            public int HzRes;
            public int VtRes;
            public int RR;
            public int Bpp;
        }




如何创建结构数组
谢谢,
shree




How do i create array of structure
thanks,
shree

推荐答案

slotInfo Array 可以如下创建:
The Array of slotInfo can be created as follows:
slotInfo[] slotInfos = new slotInfo[10];
slotInfos[0] = new slotInfo() { HzRes = 1, VtRes=2, RR = 3, Bpp=4};
slotInfos[1]=new slotInfo() { HzRes = 5, VtRes=6, RR = 7, Bpp=8};


但是,我认为最好使用如下所示的generic List<T>


But, I think it is preferable to use generic List<T> as shown below,

List<slotInfo> slotInfos2 = new List<slotInfo>();
slotInfos2.Add(new slotInfo() { HzRes = 1, VtRes=2, RR = 3, Bpp=4});
slotInfos2.Add(new slotInfo() { HzRes = 5, VtRes=6, RR = 7, Bpp=8});


因为它可以容纳项目而无需预先定义大小.因此,列表可以根据需要增长.

对于数组,如果元素数超过声明的数组大小,则必须创建一个具有新大小的新数组,并且必须将旧元素转移到新数组,如此处所述http://msdn.microsoft.com/en-us/library/bb348051(v=vs. 90).aspx [


since it accommodates the items without pre defining the size. So, the List can grow according to the requirement.

Whereas in case of the array if the number of elements exceed the size of the array declared, then a new array with new size has to be created and the old elements have to be transferred to the new array as explained here http://msdn.microsoft.com/en-us/library/bb348051(v=vs.90).aspx[^]

However, if an array is specifically required then it can be obtained from the List<T> using the ToArray method as shown below:

var slotInfoArray = slotInfos2.ToArray();


这篇关于如何创建结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆