The difference between class and struct usage and performance in C #

Source: Internet
Author: User

1.Class is a reference type and struct is a value typeThe difference between a value type and a reference type these two articles speak very well http://www.cnblogs.com/tonney/archive/2011/04/28/2032205.html http://blog.csdn.net/ liulong1567/article/details/50678930 Although we are mostly reference types in the Framework class library in. NET, our programmers use most of the value types.

Reference types such as: String,object,class are always allocated from the managed heap , and the new operator in C # returns the memory address of the object-that is, the memory address that points to the object data.

The following is a table of value types and reference types:As you can see from this picture, Class (classes)An instantiated object that points to the Memory HeapSpace allocated in the struct (structure)The object that is instantiated is the Memory StacksDistribution in Therefore, the difference between a value type and a reference type is: 1, they are stored in different locations 2, if it is a reference type, when two objects point to the same place, modify one of the time, the value of other objects will changeWhen it comes to referencing an instance of a class, the actual process is to get a pointer that points to the object's address in memory, and then passes the pointer. This is important because an instance of a class can actually be large, containing many domains and even other objects. In this case, assigning and passing the entire instance can have a very good impact on performance, which is why it is replaced with a pass-through address. When it comes to passing a value, the actual process is to make a full clone/copy of the variable, and then pass the copy with the original value intact. A struct is a value type, which is a value. This means that the structure is the ideal small data structure. Because the reference type is allocated on the managed heap, it is only cleaned up when the garbage collection is called. Value types are actually allocated on the memory stack, which means they are easily recycled and not affected by garbage collection. Data types are separated into value types and reference types. Value types are either stack-allocated or are allocated inline in the structure. The reference type is heap-allocated. Both the reference type and the value type are from the final base class Objectderived from it. When a value type needs to act as an object, it is assigned a wrapper on the heap (which makes it look like a reference object) and copies the value of that value type to it. The wrapper is tagged so that the system knows it contains a value type. This process is called boxing, and its reverse process is known as unboxing. Boxing and unboxing enable any type to be treated like an object. 2.Class can inherit from the parent class, the struct can notAll structs inherit the System.ValueType parent class by default, so you cannot inherit from another parent class, ValueType is the base class for the value type, see: https://msdn.microsoft.com/zh-cn/library/ System.ValueType (vs.80). aspx 3.Struct must assign values to all variables in the constructorAll variables in the struct must be initialized in the constructor. 4.Struct No default constructorstruct does not allow arguments to be null constructors The advantages and disadvantages of 5.Struct and class performanceAbout the value type and the reference type of memory can see this article: https://msdn.microsoft.com/zh-cn/dd365372 value type after the instantiation of the size of the memory is its all content size, that is, the contents of the larger memory, the greater the memory, stored in the stack, but the value is faster, do not need GC recycle reference type to store the value in the heap, the reference exists in the stack, when instantiated to take value in the heap, it is more time consuming, but more memory, because only the size of reference pointers, the need for GC recycling
Value type Reference type
Memory Big Small
Take Small Big
Gc Small Big

6.Struct type variable default is not emptyThat is, you cannot write this statement struct! = NULL, if you do so, be sure to add a value to it, to determine if it is empty to use hasvaluestruct? struct = *****;if (struct. HasValue) struct. value.***** = * * *; reason reference this article: the http://blog.csdn.net/xiaojie_cp/article/details/45892325 value type is followed by a question mark to indicate nullable null (Nullable structure  Nullable is a new technology provided in. NET 2.0 that indicates whether a value type can be empty.  For a type, if you can assign a value to it, you can assign it a null reference, NULL (indicating that there is no value), and we say that the type is nullable. Therefore, a nullable type can represent a value, or it indicates that no value exists. For example, a String-like reference type is a nullable type, and a value type similar to Int32 is not a nullable type. The Nullable structure supports extending a value type to be nullable, but not supported on reference types, because the reference type itself is nullable. Because a value type has only enough capacity to represent a value that is appropriate for that type, it cannot be empty, and the value type has no additional capacity required to represent a null value. Example: public int? Age

Equivalent nullable<int>

7. OtherA value type also has an attribute that once the value is modified, it produces a copy of the value type that modifies the value, does not produce a copy, but all the values that have the reference will be modified 8.eg

Let's take a look at the following code:

first, declare in class a class , and A struct structure ,

and invoke them at the program entrance, :

Now let's take a look at how they are stored in memory.

As you can see from this graph, objects instantiated by Class(classes) point to the allocated space in the memory heap

objects that are instantiated by struct (struct) are allocated in the memory stack

Next, let's make the following changes to the above program:

red box, the code defines a class instantiation object S2, and assigns the object S1 to S2

blue Box, the code defines a struct instantiation object R2, and assigns the object R1 to R2

So what is the result of their output? Please select ()

A, the value of S1 is: The value of S2 is 222
The value of R1 is: The value of R2 is 666

B, the value of S1 is: The value of S2 is 222
The value of R1 is: The value of 666 R2 is 666

C, the value of S1 is: the value of 222 S2 is 222
The value of R1 is: The value of R2 is 666

D, the value of S1 is: the value of 222 S2 is 222
The value of R1 is: The value of 666 R2 is 666

The correct answer is: C

Why is that? So let's take a look at how multiple value types and reference types are stored in memory,

, as you can see, the two reference type S1,S2 all point to the space on the same trailer heap,

When a change occurs, it changes in the same way.

The structure is a value type, although using R2=R1, the R1 object is assigned to R2,

But it allocates a separate space in the thread stack,

When you modify the value of an object, it does not affect the other object

Therefore, the difference between a value type and a reference type is:

1, they are stored in different locations

2, if it is a reference type, when two objects point to the same place, modify one of the time, the value of other objects will change

case code Download

The difference between class and struct usage and performance in C #

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.