C# 如何定义类类型的数组?

如题所述

C#中自定义类数组和结构数组的使用
发现很多时候给定的数组要实现某个逻辑或处理很是麻烦,一维数组,二维数组,,,等等需要经过n多转换,还不如自己写一个自定义数组,既方便又节省时间,以下是类数组,其实相当于定义了一个实体类一样,只是使用的时候写成数组的形式在用
Class RGB
{
public byte red;
public byte green;
public byte blue;
public RGB(byte r,byte g,byte b)
{
this.red = r;
this.green = g;
this.blue = b;
}
}
以上定义了形如实体类一样的一个类
类数组在使用的时候需要注意的是:必须要实例化
Class Test
{
//类数组
RGB[] rgb = newRGB[image.width*image.height];
byte red,green,blue;
rgb[0] = newRGB(red,green,blue);
rgb[1].red = red;
rgb[1].green = green;
rgb[1].blue = blue;
rgb[2].red = red;
...
//这样就可以使用了
}
下面是定义一个结构体
struct HSI
{
public int hue;
public int saturation;
public int intensity;
}
Class Test2
{
HSI[] hsi = new HSI[image.width*image.height];
int hue;
int saturation;
int intensity;
hsi[0].hue = hue;
hsi[0].saturation = saturation;
hsi[0].intensity = intensity;
hsi[1].hue = hue;
...
//这样使用结构数组
}
综上所述,就自定义类数组和自定义结构数组的简单使用做个总结,以便以后使用的时候注意,避免同样的错误
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-04
Student[] 学生对象的数组
List<Student> stu=new List<Student>();学生对象的集合本回答被提问者采纳
第2个回答  2010-08-18
class c
{
...
}
class main
{
public void main1()
{
c[] C=new c[15];//这样就建立了15个c类的实例
}
}
第3个回答  2010-08-18
就把原来的数据类型名改成类名。
第4个回答  2010-08-26
实体类:Model
List<Model> m=new List<Model>();
相似回答