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