In general, the use of structs is not recommended.
The structure can contain many different types of fields, methods
Define a structure:
1 Public struct Person2 {3 Public string_name;//The naming convention field is underlined before4 Public int_age;5 Public Char_gender;6 PublicQqstate _state;//Enum Type7}
struct declaration: Calling
1 // declares a struct variable, essentially the same as an enumeration, where int is a variable, 2 Person Zhangsan; 3 " Zhangsan " ; 4 - ; 5 Zhangsan._state = qqstate.busy; 6 ' male ';
The structure can also have methods:
1 Public struct Person2 {3 Public string_name;//The naming convention field is underlined before4 Public int_age;5 Public Char_gender;6 PublicQqstate _state;//Enum Type7 Public voidRun ()8 {9Console.WriteLine ("Run");Ten}
Call:
Zhangsan. Run ();
Structural Features:
Structs and classes are different and cannot be new objects. Declaring objects directly can
Structure is a value type, so the object is only in the stack and does not open space in the heap
struct does not have a default constructor.
Structure Construction Method: You must assign values to all the fields in the structure to
1 /// <summary>2 ///case with no fields3 /// </summary>4 Public structStudent5 {6 PublicStudent ()7 {8 9 }Ten Public voidStudy () One { A - } -}
Structure New object:
Student s = new Student ();
1 /// <summary>2 ///structure with Fields3 /// </summary>4 Public struct Person5 {6 Public string_name;//The naming convention field is underlined before7 Public int_age;8 Public Char_gender;9 PublicQqstate _state;//Enum TypeTen One PublicPerson (stringname) A { - This. _name =name; - This. _age = A; the This. _gender ='male'; - This. _state =qqstate.busy; - - } + - +}
C#_ basic Structure struct (eight)