轉至:https://www.cnblogs.com/ching2009/p/4293591.html
如有雷同,不勝榮幸,若轉載,請註明
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;
...
//這樣使用結構數組
}
綜上所述,就自定義類數組和自定義結構數組的簡單使用做個總結,以便以後使用的時候注意,避免犯我同樣的錯誤,歡迎大家斧正