C # Types and objects in the application of computer memory, the approximate use of two kinds of memory, one called the stack , the other called the managed heap , below we use a rectangular rectangle to represent the stack, with rounded rectangles to represent the managed heap.
First, we discuss how to store the internal variables.
First, for example, there are two methods, method_1 and add, respectively, as follows:
public void Method_1 ()
{
int value1=10; 1
int value2=20; 2
int Value3=add (value,value); 3
}
public int Add (int n1,int n2)//4
{
Rnt SUM=N1+N2;//5
Return SUM;//6
}
The execution of this code is represented by a graph:
Each of the above pictures, basically corresponds to each step in the program. At the beginning of the execution of the Method_1, the first value1 to the top of the stack, then value2, followed by the call method add, because the method has two parameters is N1 and N2, so the N1 and N2 are pressed into the stack, because here is called a method, and the method has a return value, So here we need to save the return address of the add, and then into the Add method inside, inside the add, the first is to assign a value to sum, so the sum is pressed into the stack item, and then return with return, at this time, the previous return address has played a role, return will be returned according to the address, in the process of return, Put sum to the top of the stack, find the return address, but in the Method_1 method, we want to assign the return value of add to Value3, at this time the return address is also rolled out the stack, the value3 pressed into the stack. Although the results of this example are not very useful here, this example is a good illustration of how variables and access to the stack are when the method is executed. It is also possible to see why the internal variables of the method are used later and cannot be accessed in other ways.
Next, consider the case of classes and objects in the managed heap and stack.
Check the code First:
Class Car
{
public void Run ()
{
Console.WriteLine ("All Normal");
}
Public virtual double GetPrice ()
{
return 0;
}
public static void Purpose ()
{
Console.WriteLine ("Manned");
}
}
Class Bmw:car
{
public override double GetPrice ()
{
return 800000;
}
}
There are two classes, one father a Son,son inherits father, because there is a virtual Buyhouse method in your class, so the son class can override this method.
Next, look at the calling code.
public void method_a ()
{
Double CARPRICE;//1
Car car = new BMW ();//2
Carprice = car. GetPrice ();//Call virtual method (actually call the overridden method)
Car. Run ();//Invoke instantiation method
Car.purpose ();//Call static method
}
This method is also relatively simple, that is, to define a variable to get the price, while defining a parent class of variables, with subclasses to instantiate it.
Next, we take steps to explain.
Take a look at the run-time stack and the managed heap for the love of me:
It should be explained here that the class is located in the managed heap, each class is divided into four classes, class pointers, used to correlate objects, synchronous indexes to complete synchronization (such as thread synchronization) to be established, static members are classes, so in the class, there is a list of methods (here the method list items corresponding to the specific method).
When the first step of the Method_a method is executed:
There is no value for the Carprice.
When the Method_a method executes to the second step, in fact the second step can be divided into
Car car;
Car = new BMW ();
Look at car car first;
Car here is a variable inside a method, so it is pressed onto the stack.
Look at car again = new BMW ();
This is an instantiation process, and car becomes an object
Here is a subclass to instantiate the parent type. The object is actually of the type of the subclass, but the type of the variable is the parent class.
Next, call Car.getprice () in the call in Method_a, for car, this method is a virtual method (and the subclass overrides it), the virtual method does not execute the method on the type in the call, that is, the virtual method in the car class is not executed, but the method on the corresponding class of the object is executed. That is the gtprice in BMW.
If the method run () is executed in method_a, because run is a normal instance method, the Run method in the car class is executed.
If the purpose method of method_a is called, that is, the variable car is not called, and the object is not called, it is called with the class name car, because the static method allocates memory in the class. If you generate multiple instances with car, only one copy of the static member is in the class, not in the object.
Types, objects, stacks, and managed heaps