Class:
Class is a reference type that is allocated on the heap, and instances of the class are assigned only to duplicate the reference, pointing to the memory allocated by the same segment of the actual object
Class has constructs and destructors
Class can inherit and be inherited
Structure:
The structure is the value type is allocated on the stack (although the stack's access speed is faster than the heap, but the stack's resources are limited), the assignment of the structure will be assigned to generate a new object.
Structs do not have constructors, but they can be added. struct does not have a destructor
Structs may not inherit from another structure or be inherited, but can inherit from interfaces like classes
Example:
Based on the above comparison, we can draw some lightweight objects preferably using the structure, but the data is large or complex processing logic objects best use the class.
For example: Geoemtry (An introduction in GIS, defined in the OGC standard) it is best to use the class, while the members of the Geometry midpoint are best to use the structure
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Example16
{
Interface IPoint
{
Double X
{
Get
Set
}
Double Y
{
Get
Set
}
Double Z
{
Get
Set
}
}
Structs can also inherit from interfaces
struct Point:ipoint
{
Private double x, y, Z;
Structs can also increase constructors
Public point (Double X, double Y, double Z)
{
this.x = x;
This.y = y;
This.z = Z;
}
Public double X
{
get {return x;}
set {x = value;}
}
Public double Y
{
get {return x;}
set {x = value;}
}
Public double Z
{
get {return x;}
set {x = value;}
}
}
This simplifies the design of point-like geometry, which also contains complex operations such as project (coordinate transformation).
Class Pointgeometry
{
private point value;
Public Pointgeometry (double X, double Y, double Z)
{
Value = new Point (X, Y, Z);
}
Public Pointgeometry (point value)
{
The assignment of the structure will allocate new memory
This.value = value;
}
Public double X
{
get {return value. X }
set {this.value.x = value;}
}
Public double Y
{
get {return value. Y }
set {this.value.y = value;}
}
Public double Z
{
get {return value. Z }
set {this.value.z = value;}
}
public static pointgeometry operator + (pointgeometry left, Pointgeometry rigth)
{
return new Pointgeometry (Left.x + rigth.x, Left.y + rigth.y, left.z + rigth.z);
}
public override string ToString ()
{
return string. Format ("X: {0}, Y: {1}, Z: {2}", value.) X, value. Y, value. Z);
}
}
Class Program
{
static void Main (string[] args)
{
Point tmppoint = new Point (1, 2, 3);
Pointgeometry tmpPG1 = new Pointgeometry (tmppoint);
Pointgeometry tmpPG2 = new Pointgeometry (tmppoint);
tmppg2.x = 4;
TMPPG2.Y = 5;
Tmppg2.z = 6;
Because structs are value types, the coordinates of TMPPG1 and tmpPG2 are not the same
Console.WriteLine (tmpPG1);
Console.WriteLine (tmpPG2);
Because the class is a reference type, modifying the tmpPG1 coordinates affects the tmpPG3
Pointgeometry tmpPG3 = tmpPG1;
tmppg1.x = 7;
TMPPG1.Y = 8;
Tmppg1.z = 9;
Console.WriteLine (tmpPG1);
Console.WriteLine (tmpPG3);
Console.ReadLine ();
}
}
}
Results:
X:1, Y:2, Z:3
X:4, Y:5, Z:6
X:7, Y:8, Z:9
X:7, Y:8, Z:9