1. Classes are stored in the heap. This method can ensure the flexibility of the life cycle of the issued data. Although there is some loss in performance, due to the optimization of the managed heap, the loss is relatively small.
But sometimes we only need a relatively small data structure, then we can consider using the structure
like
class windowForm
{
public int Left;
public int Right;
}
Such a class, with only two storage items, only needs to deal with these two variables. Then it can be defined as a structure
struct windowForm
{
public int Left;
public int Right;
windowForm(int left, int right)
{
this.Left = left;
this.Right = right;
}
public void set()
{
Console.WriteLine("Left:" + Left + " Right:" + Right);
}
}
In this way, structures with constructors can avoid the trouble and performance problems of defining them in a queue.
Structures can also define properties, etc.
2. The structure is a value type, stored on the stack or inline
As follows: The two Set methods process the same structure and cannot change the value of Left.
class Program
{
static void Main(string[] args)
{
windowForm win = new windowForm();
win.set();
Change(win);
win.set();
Console.Read();
}
static void Change(windowForm win)
{
win.Left = 1;
}
}
3. Structure cannot be inherited
4. The constructor of the structure is a parameterless constructor provided by the compiler, and you cannot define a new one to replace it.
5. The structure is mainly used for smaller data structures. If it is too large, there will be performance loss.
6. When passing a structure as a parameter, use the ref keyword, otherwise new content will be copied every time, causing unnecessary performance consumption.
7. When one structure is assigned to another structure, since the structure is a non-reference type, all copies will be made every time, and the class