C # Stack

Source: Internet
Author: User

Stacks are stacks, because heaps and stacks are too clumsy to say, like tongue twisters, so there are times when stacks are abbreviated as stacks. Heap and Stack, you see how comfortable this is.

But whenever the stack is not equal to heaps and stacks, it must be said that heaps and stacks or heaps and stacks.



Some people say, "C # 's reference to all value types and objects is saved in the stack, and all object instances are saved in the heap. "Right?"

Not exactly right.

It should be said that value type variables and reference type variables are stored inside the stack. Whereas a value type variable holds the value of a value type instance, a reference type variable holds a reference to the instance of the reference type.



Usually an object itself contains an instance of a value type and an object, and how are they saved?

This is where the concepts of fields and variables are understood.

In general, it can be said that the declaration in a class is a field, which is declared in a method, is a variable.
The field has an access modifier (public/private/protected/internal), and the variable does not.

Fields and variables are similar in use, but there is a big difference.

The variables are always stored inside the stack.

The field follows an instance of the class walk.

Such as:
Class ClassA
{
int i;//Field
}

Class a = new ClassA ();
Class B = new ClassA ();

Now there are actually two i,a.i and B.I.

To store an instance of a class, it is enough to actually Gencun all the characters in the class. So there is a field where the instance of the class exists.

For a class of value type, an instance of the class is stored in a variable, that is, an instance of the class follows the variable on the stack.
For classes of reference types, an instance of the class is stored on the managed heap.

Such as
struct STRUCTA
{
int i;
}

Structa a = new structa ();
At this point, the A variable holds an instance of the Structa class, which preserves all of his fields.

Structa B = A;
This time there are two structa instances, each stored in a and B, structa all the fields will be copied one copy, which means that there are now two i,a.i and B.I



You can push the class case yourself to see if you really understand.

Source for study only: http://bbs.csdn.net/topics/90039021

C # Stack

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.