English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In C#, struct is a value type data type that represents data structures. It can contain parameterized constructors, static constructors, constants, fields, methods, properties, indexers, operators, events, and nested types.
Struct can be used to store small data values that do not require inheritance, such as coordinate points, key-value pairs, and complex data structures.
The struct keyword is used to declare a structure. The default modifier is the internal modifier of the structure and its members.
The following example declares the structure of the coordinate diagram.
struct Coordinate { public int x; public int y; {}
Like basic type variables, struct objects can be created with or without the new operator.
struct Coordinate { public int x; public int y; {} Coordinate point = new Coordinate(); Console.WriteLine(point.x); //Output: 0 Console.WriteLine(point.y); //Output: 0
Above, a Coordinate (coordinate) structure object is created using the new keyword. It calls the structure's default parameterless constructor, which initializes all members to the default value of the specified data type.
If a struct type variable is declared without using the new keyword, it does not call any constructor, so all members remain in an unassigned state. Therefore, before accessing each member, they must be assigned values; otherwise, a compile-time error will occur.
struct Coordinate { public int x; public int y; {} Coordinate point; Console.Write(point.x); // Compile-time error point.x = 10; point.y = 20; Console.Write(point.x); //Output:10 Console.Write(point.y); //Output:20
A struct (structure) cannot contain a parameterless constructor. It can only contain a parameterized constructor or a static constructor.
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); //Output:10 Console.WriteLine(point.y); //Output:20
All members of the structure must be included in the parameterized constructor and assigned to the members; otherwise, if any member remains unassigned, the C# compiler will produce a compile-time error.
A struct (structure) can include properties, auto-implemented properties, methods, etc., similar to classes.
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); //Output: 0 Console.WriteLine(point.y); //Output: 0
The following struct includes static methods.
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); //Output: 0 Console.WriteLine(point.y); //Output: 0
Structures can contain event notifications to subscribers about certain operations. The following structure (struct) contains an event.
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; {}
The above structure contains the coordinateChanged event, which is triggered when the x or y coordinates change. The following example demonstrates how to handle the CoordinateChanged event.
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("The coordinates change to {0}", point); {} {}
Struct is a value type, therefore it is faster than class objects. Use struct whenever you only want to store data. Typically, structures are suitable for game programming. However, it is easier to transmit class objects than structures. Therefore, do not use struct when passing data over the network or through other classes.
struct can include constructors, constants, fields, methods, properties, indexers, operators, events, and nested types.
struct cannot contain parameterless constructors or destructors.
struct can implement interfaces that are the same as those implemented by classes.
struct cannot inherit from another structure or class, nor can it be a base class for a class.
struct cannot specify members as abstract, sealed, virtual, or protected.