Information Technology Reference
In-Depth Information
Structs Are Value Types
As with all value types, a variable of a struct type contains its own data. Consequently
A variable of a struct type cannot be
null
.
Two structs cannot refer to the same object.
For example, the following code declares a class called
CSimple
, and a struct called
Simple
,
and a variable of each. Figure 12-1 shows how the two would be arranged in memory.
class CSimple
{
public int x;
public int y;
}
struct Simple
{
public int x;
public int y;
}
class Program
{
static void Main()
{
CSimple cs = new CSimple();
Simple ss = new Simple();
...
Figure 12-1.
Memory arrangement of a class versus a struct