Information Technology Reference
In-Depth Information
Field Initializers
Field initializers are not allowed in structs.
struct Simple
{
Not allowed
↓
public int x = 0; // Compile error
public int y
= 10
; // Compile error
}
↑
Not allowed
Inheritance
Structs are always implicitly sealed, and hence, you cannot derive other structs from them.
Since structs do not support inheritance, the use of several of the class member modifiers
with struct members would not make sense; thus, they cannot be used in their declarations.
The modifiers that cannot be used with structs are the following:
• protected
• internal
• abstract
• virtual
Structs themselves are, under the covers, derived from
System.ValueType
, which is derived
from
object
.
The one inheritance-associated keyword you
can
use with a struct member is the
new
mod-
ifier, to override the implementation of a member of base class
System.ValueType
.
Boxing and Unboxing
As with other value type data, if you want to use a struct instance as a reference type object, you
must make a boxed copy. Boxing and unboxing are covered in detail in Chapter 18.