在C#中,struct是表示數(shù)據(jù)結(jié)構(gòu)的值類型數(shù)據(jù)類型。 它可以包含參數(shù)化構(gòu)造函數(shù)、靜態(tài)構(gòu)造函數(shù)、常量、字段、方法、屬性、索引器、運算符、事件和嵌套類型。
struct 可以用于保存不需要繼承的小數(shù)據(jù)值,例如坐標(biāo)點,鍵值對和復(fù)雜的數(shù)據(jù)結(jié)構(gòu)。
使用struct關(guān)鍵字聲明結(jié)構(gòu)。默認(rèn)修飾符是結(jié)構(gòu)及其成員的內(nèi)部修飾符。
下面的示例聲明坐標(biāo)圖的結(jié)構(gòu)。
struct Coordinate { public int x; public int y; }
與基本類型變量一樣,可以使用或不使用 new 運算符創(chuàng)建struct對象。
struct Coordinate { public int x; public int y; } Coordinate point = new Coordinate(); Console.WriteLine(point.x); //輸出:0 Console.WriteLine(point.y); //輸出:0
上面,使用new關(guān)鍵字創(chuàng)建一個 Coordinate(坐標(biāo)) 結(jié)構(gòu)的對象。它調(diào)用結(jié)構(gòu)的默認(rèn)無參數(shù)構(gòu)造函數(shù),該構(gòu)造函數(shù)將所有成員初始化為指定數(shù)據(jù)類型的默認(rèn)值。
如果聲明一個 struct 類型的變量而不使用 new 關(guān)鍵字,則它不調(diào)用任何構(gòu)造函數(shù),因此所有成員都保持未賦值狀態(tài)。因此,在訪問每個成員之前,必須為它們分配值,否則會產(chǎn)生編譯時錯誤。
struct Coordinate { public int x; public int y; } Coordinate point; Console.Write(point.x); // 編譯時錯誤 point.x = 10; point.y = 20; Console.Write(point.x); //輸出:10 Console.Write(point.y); //輸出:20
結(jié)構(gòu)(struct)不能包含無參數(shù)構(gòu)造函數(shù)。它只能包含參數(shù)化構(gòu)造函數(shù)或靜態(tài)構(gòu)造函數(shù)。
struct Coordinate { public int x; public int y; public Coordinate(int x, int y) { this.x = x; this.y = y; } } Coordinate point = new Coordinate(10, 20); Console.WriteLine(point.x); //輸出:10 Console.WriteLine(point.y); //輸出:20
必須在參數(shù)化構(gòu)造函數(shù)中包含該結(jié)構(gòu)的所有成員,并將其分配給成員;否則,如果任何成員保持未分配狀態(tài),C#編譯器將給出編譯時錯誤。
struct(結(jié)構(gòu))可以包含屬性、自動實現(xiàn)的屬性、方法等,與類相同。
struct Coordinate { public int x { get; set; } public int y { get; set; } public void SetOrigin() { this.x = 0; this.y = 0; } } Coordinate point = Coordinate(); point.SetOrigin(); Console.WriteLine(point.x); //輸出:0 Console.WriteLine(point.y); //輸出:0
以下struct包括靜態(tài)方法。
struct Coordinate { public int x; public int y; public Coordinate(int x, int y) { this.x = x; this.y = y; } public static Coordinate GetOrigin() { return new Coordinate(); } } Coordinate point = Coordinate.GetOrigin(); Console.WriteLine(point.x); //輸出:0 Console.WriteLine(point.y); //輸出:0
結(jié)構(gòu)可以包含事件通知訂閱者有關(guān)某些操作。 下面的結(jié)構(gòu)體(struct)包含一個事件。
struct Coordinate { private int _x, _y; public int x { get{ return _x; } set{ _x = value; CoordinatesChanged(_x); } } public int y { get{ return _y; } set{ _y = value; CoordinatesChanged(_y); } } public event Action<int> CoordinatesChanged; }
上面的結(jié)構(gòu)包含coordinateChanged事件,當(dāng) x 或 y 坐標(biāo)發(fā)生變化時將引發(fā)該事件。下面的示例演示如何處理CoordinateChanged事件。
class Program { static void Main(string[] args) { Coordinate point = new Coordinate(); point.CoordinatesChanged += StructEventHandler; point.x = 10; point.y = 20; } static void StructEventHandler(int point) { Console.WriteLine("坐標(biāo)更改為 {0}", point); } }
struct是值類型,因此它比類對象快。每當(dāng)您只想存儲數(shù)據(jù)時,請使用struct。通常,結(jié)構(gòu)適用于游戲編程。但是,與結(jié)構(gòu)相比,傳輸類對象更容易。因此,當(dāng)您通過網(wǎng)絡(luò)或其他類傳遞數(shù)據(jù)時,請不要使用struct。
struct 可以包括構(gòu)造函數(shù),常量,字段,方法,屬性,索引器,運算符,事件和嵌套類型。
struct 不能包含無參數(shù)構(gòu)造函數(shù)或析構(gòu)函數(shù)。
struct 可以實現(xiàn)與類相同的接口。
struct 不能繼承另一個結(jié)構(gòu)或類,也不能作為一個類的基類。
struct 不能將成員指定為抽象成員,密封成員,虛擬成員或受保護(hù)成員。