Structures are defined using the struct keyword, for example:
public struct PostalAddress { // Fields, properties, methods and events go here... }
Structures share most of the same syntax as classes, but structures are more restrictive than classes:
- In a structure declaration, it cannot be initialized unless the field is declared as const or static.
- Structures cannot declare default constructors (constructors without arguments) or destructors.
- Structures are copied when assigned. When you assign a structure to a new variable, all data is copied and any modifications made to the new copy do not change the data of the original copy. Be sure to keep this in mind when using collections of value types (such as Dictionary<string, myStruct>).
- Structures are value types, while classes are reference types.
- Unlike classes, the instantiation of a structure can be done without the new operator.
- Structures can declare constructors with parameters.
- A structure cannot be inherited from another structure or class, and cannot be used as the basis of a class. All structures are inherited directly from , the latter from .
- The structure can implement interfaces.
- A structure can be used as a type that can be null, so it can be assigned a null value.
The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is equally convenient to represent a point as a class using automatically implemented properties, using structures is more efficient in some cases. For example, if you declare an array of 1000 Point objects, more memory is allocated to reference each object; in this case, using structures can save resources. Because the .NET Framework contains an object named Point, the structure in this example is named "CoOrds".
public struct CoOrds { public int x, y; public CoOrds(int p1, int p2) { x = p1; y = p2; } }
It is wrong to define a default (no parameter) constructor for the structure. Initializing the instance field in the structure is also wrong. Structural members can only be initialized in two ways: one is to use parameterized constructors, and the other is to access members separately after declaring the structure. For any private member or member that is otherwise set to inaccessible, it can only be initialized in the constructor.
If you use the new operator to create a structural object, the structural object is created and the appropriate constructor is called. Unlike classes, the instantiation of a structure can be done without the new operator. In this case, there is no constructor call, so allocation efficiency can be improved. However, until all fields are initialized, the fields remain unassigned and the object is unavailable.
When a structure contains a reference type as a member, the default constructor of that member must be called explicitly, otherwise the member will remain unassigned and the structure is unavailable. (This will cause compiler error CS0171.)
For structures, there is no inheritance like classes. A structure cannot be inherited from another structure or class, and cannot be used as the basis of a class. However, the structure inherits from the base class Object. The structure can implement interfaces, and the same way is exactly the same.
The class cannot be declared using the struct keyword. In C#, classes and structures are semantically different. Structures are value types, while classes are reference types.
Unless reference type semantics are required, declaring smaller classes as structures can improve the processing efficiency of the system.
Example 1
describe
The following example demonstrates struct initialization using the default constructor and parameterized constructor.
Code
public struct CoOrds { public int x, y; public CoOrds(int p1, int p2) { x = p1; y = p2; } } // Declare and initialize struct objects. class TestCoOrds { static void Main() { // Initialize: CoOrds coords1 = new CoOrds(); CoOrds coords2 = new CoOrds(10, 10); // Display results: ("CoOrds 1: "); ("x = {0}, y = {1}", , ); ("CoOrds 2: "); ("x = {0}, y = {1}", , ); // Keep the console window open in debug mode. ("Press any key to exit."); (); } }
Output:
CoOrds 1: x = 0, y = 0 CoOrds 2: x = 10, y = 10
Example 2
describe
The following is an example of a unique function of the structure. It creates a CoOrds object without using the new operator. If you change struct to class, the program will not compile.
Code
public struct CoOrds { public int x, y; public CoOrds(int p1, int p2) { x = p1; y = p2; } } // Declare a struct object without "new." class TestCoOrdsNoNew { static void Main() { // Declare an object: CoOrds coords1; // Initialize: = 10; = 20; // Display results: ("CoOrds 1: "); ("x = {0}, y = {1}", , ); // Keep the console window open in debug mode. ("Press any key to exit."); (); } }
Output:
CoOrds 1: x = 10, y = 20