Detailed description of the structure struct in C #

Source: Internet
Author: User

I. Differences between structures and Classes

1. The structure level is consistent with that of the class. It is written under the namespace. You can define fields, attributes, methods, and constructor methods, or create an object using the keyword new.

2. Fields in the structure cannot be assigned an initial value.

3. A constructor without parameters is automatically generated by the C # compiler no matter how it is. Therefore, a constructor without parameters cannot be defined for the structure.

4. In the constructor, all fields of the struct must be assigned a value.

5. assign values to the attribute in the constructor. It is not considered to assign values to the field, because the attribute is not necessarily a field to be operated.

6. The structure is a value type. When a structure variable is transferred, each field in the structure object is copied to the field of the new structure variable.

7. Automatic attribute cannot be defined because the field property generates a field which must be in the constructor, but we do not know the name of this field.

8. Declare the struct object without using the new keyword. However, the struct object field does not have an initial value. Because the constructor is not called, the field must be assigned a value in the constructor, create a struct object using the new keyword. The fields of this object have default values.

9. The stack access speed is fast, but the space is small, and the access speed of the stack is slow, but the space is large. When we want to represent a lightweight object, it is defined as a structure, to increase the speed, select based on the impact of the transfer. If you want to transfer a reference, it is defined as a class. If you want to transfer a copy, it is defined as a structure.

Ii. Demo

                                               {               { x =                                     {               { y =                         Console.Write(, .X,            Point( x,              .x =             .y =             .p =            Point(              .x =             .y =              .p =            Point( x,               .x =             .y =             .p =                   Name { ;            Main(                                                                                                                                                               Program p =  Program() { Name=             Point point1 =  Point(,              Point point2 =             point2.p.Name =              Console.WriteLine(point1.p.Name);      }

 

 

Note: All the above articles are original articles. If you need to reprint them, please indicate the source!

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.