C # 's first knowledge structure (struct)

Source: Internet
Author: User

C # structure (struct)

The first structure is the value type data structure . It enables a single variable to store relevant data for various data types. The struct keyword is used to create structures. Popular says: A structure is a collection that can contain different data types. It is a data type that can be defined by itself.

Enumerations and structs are all value types, enumerations are often "literal" in real-life problems, easy to understand and intuitive, and second enumerated types can limit a type to a controllable range. And what about the structure? The structure makes it easy to handle collections of different data types, such as registering information for 100 students.

For example, the structure of defining student information is as follows: Initialize assignment considerations and learn later

 1  public  struct  MyStruct //  My students  2   { 3  public  string  Name; //  name  4  public    String  mobilephone; //  mobile phone    5  public  DateTime Birthday; //  birthday  6 } 

  To declare the information of Zhang Fei as follows, it is simple to declare the information of 100 students.

1             mystruct P1; 2             " Zhang Fei " ; 3             " 15358905577 " ; 4             P1. Birthday = DateTime.Now.AddYears (-);

  A struct variable can be assigned to the same structure variable, and all the data held by that struct variable will be copied at the same time, and the following is a statement of the student information variable code as follows, P2 all data types to P1.

1 mystruct P1;2 mystruct P2;3P1. Name ="Zhang Fei";4P1. Mobilephone ="15358905577";5P1. Birthday = DateTime.Now.AddYears (- -);6             //assigns the value of the P1 to P2//because it is a value type, the assignment is equal to the stack space where all values are copied to P27P2 = P1;

C # 's first knowledge structure (struct)

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.