[You must know. NET] The fourth time: from behind: Class and struct

Source: Internet
Author: User
Tags abstract constructor garbage collection inheritance thread

1. Introduction

To mention class and struct, our first feeling is that the grammar is almost the same, but the treatment is very much the same. History passes the baton from process-oriented programming to object-oriented programming, and class and struct also carry their own destinies. In my opinion, the struct hero, the class world alone, the most essential difference is that class is a reference type, and struct is a value type, their allocation in memory is different. resulting in a series of differences, this article will be done in full discussion.

2. Basic Concepts

2.1. What is class?

Class is the basic concept of object-oriented programming, a custom data structure type that usually contains fields, properties, methods, properties, constructors, indexers, operators, and so on. Because it is the basic concept, so do not need to describe this in detail, the reader can query the relevant concepts understand. Our emphasis is on the importance of. NET, all classes eventually inherit from the System.Object class, and therefore are a reference type, that is, when an instance of a new class is used, the object holds the reference address of the actual data of the instance, and the value of the object is kept in the managed heap (managed heap).

2.2. What is struct?

A struct (structure) is a value type that organizes a set of related information variables into a single variable entity. All structures inherit from the System.ValueType class, so it is a value type, that is, the struct instance allocates the stack on the thread, which itself stores the value, and does not contain a pointer to the value. So when using struct, we can treat it as a basic type class such as int and char.

3. Same point, different point

Same point: syntax similar.

Different points:

class is a reference type that inherits from the System.Object class, struct is a value type, inherits from the System.ValueType class, and is therefore not polymorphic. Note, however, that System.ValueType is a reference type.

• From a functional standpoint, class behaves as behavior, and struct is often used to store data.

class supports inheritance, can inherit from classes and interfaces, and struct has no inheritance, struct cannot inherit from class, nor can it be the base class of class, but struct supports interface inheritance (remember, the second time: discussion of abstract programming: interfaces and abstract classes)

class can declare an parameterless constructor, you can declare a destructor, and struct can only declare a constructor with a parameter, and you cannot declare a destructor. Therefore, struct does not have a custom default parameterless constructor, the default parameterless constructor simply initializes all values to their 0 value

• When instantiated, class uses the new keyword, and struct can not use the new keyword, struct the initialization process when declared, all member variables default to 0 or null.

class can be real abstract classes (abstract), can declare abstract functions, and struct is abstract and cannot declare abstract functions.

class can declare protected members, virtual members, sealed members, and override members, while struct may not, but it is noteworthy that struct can overload 3 virtual methods of System.Object, Equals () , ToString (), and gethashtable ().

class object replication is divided into shallow copies and deep copies (the topic we will focus on later in this series, which is not detailed in this article), must be done in a special way to replicate, and the objects created by struct are simple to copy and can be connected directly to the equals sign.

The class instance is a garbage collection mechanism to ensure that the memory is recycled, and the struct variable is automatically released as soon as it is used.

• When passed as a parameter, the class variable is passed by address, and the struct variable is passed in a value-by-way.

We can simply understand that class is a moving machine with behavior, polymorphism, and inheritance, while struct is a part box that combines parts of different structures. In fact, the most essential difference between class and struct is that class is a reference type, memory is allocated to the managed heap, and struct is a value type, and memory is allocated on the thread's stack. This difference leads to all of the above differences, so only a deep understanding of the relevant content of memory allocation in order to better control. This series will be in the future content, reference types and value types to do in-depth comparison and discussion, please pay attention to. Of course, as the title described in this article, the use of class can be used to replace the struct of any occasion, class from behind. Although in some respects struct has the performance aspect superiority, but in the object-oriented programming, basically is the class rampant world.

So, some people will suggest that, since class can almost completely replace struct to achieve all the functions, then struct still have the necessary? The answer is that, at least in the following cases, we should consider using struct instead of class, given the performance considerations:

• Struct can be considered when implementing a structure that is primarily used to store data.

The struct variable occupies the space of the stack, so it only applies to situations where the volume of data is relatively small.

• An array of structures is more efficient.

• Provides compatibility for some and unmanaged code communications.

All of these are reasons why struct has a place, and of course there are other things, but I don't know:-)

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.