码迷,mamicode.com
首页 > 编程语言 > 详细

C#中自定义类数组和结构数组的使用

时间:2015-02-16 00:19:13      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

如有雷同,不胜荣幸,若转载,请注明

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;

...

//这样使用结构数组

}

综上所述,就自定义类数组和自定义结构数组的简单使用做个总结,以便以后使用的时候注意,避免犯我同样的错误,欢迎大家斧正

C#中自定义类数组和结构数组的使用

标签:

原文地址:http://www.cnblogs.com/ching2009/p/4293591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!