Possible Duplicate:
Structure Vs Class in C#
Sorry if this is a open ended question, but I just want to know if my struct is too big. The reason why i want to use a struct is because I've learned that they're faster then classes and I really need the speed, I think.
I found out that if your struct is too big it actually slows down your program, so I want to know whats the guideline on this and whether or not my struct needs to be converted to a class or not.
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;
}
}
Should I change it to a class? It's getting kinda bloated to me. I'm using Visual Studio 2008.
Advice to non-blackbelt programmers: don't optimise your code.
Advice to blackbelt programmers: optimise your code later.
I seriously recommend you heed this advice!
To give you some more pertinent explanation, you should be aware that, unlike classes, structs are allocated on the stack, not on the heap. Stack allocation is slightly cheaper, but is usually only appropriate for small, objects that are either short-lived or which are passed by value or which are going to be stored in a large array.
If you don't know what passed by value means, it means that the entire struct is copied whenever you pass it as an argument to a method. This can quickly become very expensive if your structs are not very small. It can also lead to unexpected behaviour if you don't understand this distinction: changes made to a class argument in a method are visible to the method caller; changes made to a struct argument in a method are not visible to the method caller (because the method is changing a copy of the original data).
To reiterate my first point: don't use structs unless you're sure you know what you're doing.
Hope this helps!
Size isn't the reason to choose a struct over a class. Use struct ONLY if you want value semantics. Think about an int; 5 is a 5 is a 5, references don't matter. structs are always passed by making a copy.
Use a struct when you want value semantics. Otherwise, use a class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With