可能重复:
C#中结构体与类的区别
如果我的结构体太大,是否会影响效率,这个问题可能没有定论。我想使用结构体是因为我了解到它们比类更快,而且我确实需要速度。
我发现如果结构体太大,实际上会减慢程序的运行速度,所以我想知道有关这方面的指导方针,以及是否需要将我的结构体转换成类。
public struct Tiles
{
public Rectangle rect;
//i know this wont run with them being initalized like this but if i change to a class this will
//stay like this and im in the middle of deciding what to do
Bitmap currentPic = new Bitmap(50, 50);
ImageAttributes imgAttr = new ImageAttributes();
float[][] ptsArray;
ColorMatrix clrMatrix;
public void setTiles(int i, int k, int width, int height)
{
Rectangle temp = new Rectangle(i, k, width, height);
rect = temp;
float[][] ptsTemp ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, .9f, 0},
new float[] {0, 0, 0, 0, 1}};
ptsArray = ptsTemp;
clrMatrix = new ColorMatrix(ptsArray);
currentPic = The_Great_Find__Dig_Deeper.Properties.Resources.darknessflashlight;
}
public void setTransperancy(float value)
{
clrMatrix.Matrix33 = value;
}
public void drawRect(Graphics g)
{
imgAttr.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(currentPic, rect, 0, 0, currentPic.Width, currentPic.Height, GraphicsUnit.Pixel, imgAttr);
}
bool blackened
{
get;
set;
}
}
我应该将它改成一个类吗?对我来说它变得有点臃肿了。我正在使用Visual Studio 2008。