Let's give you a New Year! Happy New Year ~~
Here, if you read this study note and read it at the beginning, you will be broken by my previous wrong thinking.
This is a bit confusing.
Meow once had these ideas:
The first type is the reference type.
2. Use new to allocate memory in the heap
All types are inherited from system. object.
4. C # is an object-oriented language. Everything is based on classes.
The five value type is on the stack, and the reference type is on the stack.
Now let's take a look at some of my conflicts.
FirstClass is a reference type. All types are based on the class int type, and the class is a reference type. So how can an int be stored on the stack?
SecondNew is to allocate memory in the heap, so int A; int A = new int (); what are the situations in which the former is in the stack and the latter is in the heap?
Let's take a look at the reference type and value type.
The common saying is that the reference type has a pointer on the stack that is linked to the heap memory, and the value type is the value in the stack memory.
But it is far from that simple ifA value type is used in a reference type.What about it?
So I found out this article: http://blog.csdn.net/lerit/archive/2009/08/13/4441239.aspx
After reading this article, meow said that the previous ideas were just a piece of work.
• For value-type instances, CLR has two allocation methods at runtime: (1) if the value-type instance is used as a local variable in the method of the type, the instance is created on the thread stack. (2) If an instance of the value type is a member of the type, the instance is created on the GC stack as part of the instance of the reference type (the reference type is created on the GC heap or LOH.
Okay .. meow found that he had always thought that he was always thinking about the value type on the stack. The reference type was always on the stack, and then he thought that there were contradictions in many places and he had been confused about the ideas. unable to tangle with this... we should focus more on the differences between the value type and the reference type, rather than on where they are allocated. net
Next let's take a lookUse new for Value TypeRight
I have been wondering if I have not found the answer.
Finally, I saw this sentence a few days ago when I read the C # Essence:
The new operator is called for a reference type. The newobj. New command is compiled into the pencil command and can also be used for value type. However, if the bottom-layer rule is initobj, the command uses the default value to initialize the memory.
That is, the new here is the initialization function. Let's look at the Code:
Code:
- Static void main (string [] ARGs)
- {
- // Int A; Compilation failed
- Int A = new int ();
- Int B = int32.parse (console. Readline ());
- A + = B;
- Console. readkey ();
- }
Here, it is very different from C ++. The int of C ++ can be directly used as long as it is declared (although meaningless) however, in C #, it must be initialized before it can be used. If it is directly used without initialization, it will not be compiled. After new is used, the default value is used for initialization... same as default
See here I also checked the concept of C ++ and C # New presented a comparative article: http://dazhabai.blog.sohu.com/70558348.html
Then I checked out the msdn explanation for new:
-
New operator
-
Used to create objects and call constructors.
-
New Modifier
-
Used to hide an inherited member from a base class member.
-
New Constraint
-
It is used to restrict the types of parameters that may be used as type parameters in a generic declaration.
I can find that I didn't mention any of the half heap characters. So I went to the next page and found that this class is a reference type.The user-defined class is a reference type.
In this way, an endless loop will jump out.
FirstNew does not necessarily allocate memory in the heap. It is used for initialization when the value type is used.
SecondNot all classes are reference types, but some of the basic value types customized by the user are not user-defined ....
I want to draw a picture of my conclusions!
So far, I have found that the learning of. NET is extremely poor, which leads to the fact that I have never figured out why I can't think of it.
With a sigh, I feel that I am a real scum.