This is the 15th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
concept
In C#, a structure is a value-type data structure. It allows a single variable to store related data of various data types. The struct keyword is used to create a structure.
For these more complex data representations, C# provides structure types to ensure that the description of the data is as consistent as possible with the actual application.
C#’s struct types (or structures) are user-defined types that allow users to view data types that are different but related to each other as a whole.
C# structures allow you to combine different types of related data to form a new data type.
define
The definition form is:
[public] structStruct type name {member declaration; }Copy the code
-
A member declaration describes a collection of data members (or data elements) of that type, as well as members of other types.
-
The number of members can be any number, depending on the specific requirements.
-
Braces are the bounds of the list of members.
When struct type is defined, the type declaration of each data member must be given.
As follows:
publicMember type List of member names;Copy the code
Public is an access modifier that indicates that the data member is allowed to be accessed outside of the structure type definition.
If a data member was not preceded by a public access modifier when the structure was defined, then a structure-type variable cannot access the data member.
If multiple declared members are of the same type, a list of member names is formed, separated by commas.
- For example, the following structure types can be established for the representation of coordinate point information.
struct Ponit{
public int X; / / the abscissa
public int Y; / / ordinate
}
Copy the code
A structure is a data type of a value type and can be defined inside a class or outside all classes, depending on its scope of use.
In general, structure types defined inside a class are used only within that class, while structures defined outside the class can be used within the scope of the current namespace.
In C#, structure definitions cannot be placed inside methods or functions.
added
The following is a supplementary explanation of the struct type definition.
-
Struct is a keyword that defines the type of a structure and does not represent a data type.
-
The struct keyword cannot be used as the data type of a variable until the specific struct type is defined.
-
The members of a struct type can be either of a simple data type or of a struct type, that is, the definition of a struct can be nested.
Such as:
struct Ponit{
public int X; / / the abscissa
public int Y; / / ordinate
}
struct Ponit_3{
public Ponit Pos_X_Y;
public intZ; }Copy the code
- C# does not allow members inside constructs to be of this type. Such as:
struct Ponit{
public int X; / / the abscissa
public int Y; / / ordinate
// This is wrong ❎
publicPonit pos; }Copy the code
-
Structures in C# can have function members in addition to data members.
-
The biggest difference between a structure and a class is that a class is a reference type and a structure is a value type. The structure is sealed and cannot be inherited.