Black Horse programmer _ C # Structure Learning

Source: Internet
Author: User

A structure is a data type defined by a programmer and is very similar to a class. Has data members and function members. But there are also differences: 1) the class is the reference type, while the structure is the value type; 2) the structure is implicitly sealed and cannot be derived; the syntax declaration is similar to the class: // structure declaration struct StructName {// contains member variables} Read the sample code below to demonstrate the use of the C # structure: static void Main (string [] args) {Point first, second, third; first. x = 10; first. y = 10; second. x = 20; second. y = 20; third. x = first. x + second. x; third. y = first. y + second. y; Console. writeLine ("first: {0}, {1}", first. x, first. y); Console. writeLine ("second: {0}, {1}", second. x, second. y); C Onsole. writeLine ("third: {0 },{ 1}", third. x, third. y); Console. readKey () ;}struct Point {public int x; public int y;} structure is Value Type 1) structure type variables cannot be null; 2) two Structure Variables cannot reference the same object static void Main (string [] args) {CSimple cs1 = new CSimple (), cs2 = null; Simple ss1 = new Simple (), ss2 = new Simple (); cs1.x = ss1.x = 5; cs1.y = ss1.y = 10; cs2 = cs1; // value assignment class instance ss2 = ss1; // value assignment structure instance} class CSimple {public int x; public int y ;} Struct Simple {public int x; public int y;} first creates a CSimple class, and a Simple structure declares two variables for them after they are instantiated in Main, cs1 and cs2 point to references in the stack, while ss1 and ss2 allocate and store space in the stack. Assign a structure to another structure, that is, repeat the value from a structure to another structure. Unlike a class, when copying a class variable, only the reference is copied. In the code above, after the class assignment is complete, cs2 and cs1 point to the same object in the heap. However, after the Structure assignment is complete, the value of the ss2 member is the same as that of the ss1 member. The constructor and destructor in the structure provide a non-parameter constructor for each structure implicitly. This constructor sets each member of the structure to the default value of this type, and the referenced member is set to null. the predefined non-parameter constructor exists for each structure, and cannot be deleted or redefined. However, you can create another constructor as long as they have parameters. This is different from the class. For a class, the compiler only provides an implicit non-parameter constructor when there is no other constructor declaration. To call a constructor, including an implicit non-parameter constructor, use the new operator. The new operator is used even if memory is not allocated from the heap. Example: static void Main (string [] args) {Simple s1 = new Simple (); // call the implicit constructor Simple s2 = new Simple (5, 10 ); // call the constructor Console. writeLine ("{0}, {1}", s1.x, s1.y); Console. writeLine ("{0}, {1}", s2.x, s2.y); Console. readKey () ;}struct Simple {public int x; public int y; public Simple (int a, int B) {x = a; y = B ;}} it may not apply to instances where the new operator creates a structure. However, there are some restrictions: 1) You cannot use the value of the data member until it is displayed. 2) You cannot call any function member, until all data members have been assigned static void Main (string [] args) {Simple s1, s2; Console. writeLine ("{0}, {1}", s1.x, s1.y); // compilation error. s1.x, s1.y have not been assigned s2.x = 50; s2.y = 10; Console. writeLine ("{0}, {1}", s2.x, s2.y); Console. readKey () ;}struct Simple {public int x; public int y ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.