struct(C# 参考)struct (C# Reference)

struct 类型是一种值类型,通常用来封装小型相关变量组,例如,矩形的坐标或库存商品的特征。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. 下面的示例显示了一个简单的结构声明:The following example shows a simple struct declaration:

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}

备注Remarks

结构还可以包含构造函数常量字段方法属性索引器运算符事件嵌套类型,但如果同时需要上述几种成员,则应当考虑改为使用类作为类型。Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

有关示例,请参阅使用结构For examples, see Using Structs.

结构可以实现接口,但它们无法继承另一个结构。Structs can implement an interface but they cannot inherit from another struct. 因此,结构成员无法声明为 protectedFor that reason, struct members cannot be declared as protected.

有关详细信息,请参阅结构For more information, see Structs.

示例Examples

有关示例和详细信息,请参阅使用结构For examples and more information, see Using Structs.

C# 语言规范C# language specification

有关示例,请参阅使用结构For examples, see Using Structs.

请参阅See also