A structure can be thought of as
a lightweight class type having value-based semantics. Typically, structures
are best suited for modelling geometric and mathematical data, and are created
in C# using the struct keyword. A struct type is a
value type that is typically used to encapsulate small groups of related
variables, such as the coordinates of a rectangle or the characteristics of an
item in an inventory. Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. Structs can implement an interface
but they cannot inherit from another struct. For that reason, struct members
cannot be declared as protected. It is an error to
declare a default (parameterless) constructor for a struct. It is an error to
initialize an instance field in a struct. It is an error to initialize
an instance field in a struct.
A struct cannot inherit from another struct or class, and it cannot be
the base of a class. Structs, however, inherit from the base class Object. Unless you need reference type semantics, a class
that is smaller than 16 bytes may be more efficiently handled by the system as
a struct.
Unlike classes,
structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and
the object cannot be used until all of the fields are initialized.
No comments:
Post a Comment