C # struct

Source: Internet
Author: User

Very confused, why C # would have a struct such a keyword. Although I have used C # for years, I rarely use this keyword. When I learned C # in related books, I saw struct content--but C # was not my first entry language, so I didn't learn it so carefully. Part of the reason for this is that some books will downplay the struct, and the reason for that is my prior experience. The reasons for ignoring the struct keyword are as follows:

    1. Already have class, and what are the structs doing? Save memory? This is not the main selling point of C #.
    2. A variety of magical restrictions. That being the case, it's even more useless.
    3. Is it just for interoperability with C + +, Win32? Now the application is more and more away from the OS, the scene is rare encounter, or not.

Think about it. Recently as an interviewer, interviewed some candidates, another C + + born buddy, asked the struct and class difference-even memory layout, think oneself also not very clear, or understand it. C + + people, always remember the memory.

Find related information on the struct on MSDN: Strcut is a value type, and class is a reference type. When a struct is assigned, it is a copy operation. The reference type, however, simply refers to the same content. Value types are similar to function parameters in C, and reference types are similar to arguments. There are some other characteristics of value types, such as Nullable (Nullable), and so on.

There are some other features and limitations [1], but I'd like to know why there are structs.

    1. You can customize the new value type. The generic value type has only the original base type, such as int, byte.
    2. Structs are best suited to small data structures that contain data that is not modified after the structure is created. [2]
    3. [3] The structure is more efficient, which means more memory saving and improved processing efficiency. Because there are not many features of class, there is no meta-data block for class.
    4. Structs are value types, and value types are computable and have computable semantics. such as Datatime. [4]

The general scenario for using structs can be seen from the above:

    1. A custom type is semantically a value type. Stable, constant, computable.
    2. More efficient scenarios are needed.

As for the 2nd above, remember that it loses a lot of object-oriented features.

[1] Structure (C # Programming Guide), HTTPS://MSDN.MICROSOFT.COM/ZH-CN/LIBRARY/SAXZ13W4 (v=vs.90). aspx

[2] classes and Structures (C # programming), https://msdn.microsoft.com/zh-cn/library/ms173109.aspx

[3] using Structures (C # Programming Guide), https://msdn.microsoft.com/zh-cn/library/0taef578.aspx

[4] In-depth understanding C #, People's post and telecommunications press, P41-43

C # 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.