Data stored in memory depends on its data type, in C #, divided into value type and reference type, value type data stored in the stack in memory, each variable or program has its own stack You cannot share a stack address. Two different addresses are assigned in the stack when the variable of the data is passed to another variable of the same type. The data of the reference type is stored in a heap in the memory, and data can be used in different variables or programs to use the same location. When data is passed from a variable of a reference type to another variable, the reference address of this variable is passed to the new variable, and the data stored in the current stack is referenced. Detailed conclusions can be obtained by instance: use system; // Define a rectangular class, class belongs to the reference type class refRectangle {public int width; public int hotHT;} // Defines a rectangular structure, belonging to the value type Struct ValRectangle {public int width; Public int.com;}
Class REFVALRECTANGLE {public static void main () {// Create a rectangular object and passes the value to another new object. RefRectangle Ref1 = new refRectangle (); ref1.width = 3; ref1.height = 4; RefRectangle Ref3 = Ref1; console.writeline ("Dimensions of Ref1 Are:" ref3.width.tostring () "..." ref3.height.toString ()); console.writeline ("Change Dimensions of Ref1"); Ref1.width = 10; ref1.height = 50; bool btransfer = ref3.equals (ref1); console.writeline ("Dimensions of Ref1 now:" ref3.width.toString () "....." ref3.height.tostring ()); console.writeline (btransfer.tostring ()); console.readline (); // Create a rectangular structure, pass the value to a new rectangular structure VALRECTANGLE VAL1 = New VALRECTANGLE () VAl1.width = 3; val1.height = 4; valRectangle Val3 = VAL1; console.writeline ("Dimensions of val1 is:" VAL3.WIDTH.TOSTRING () "..." val3.height.tostring )); Console.writeline; val1.width = 10; val1.height = 50; BOOL BPASS = VAL3.EQUALS (VAL1); Console.writeline ("Dimensions of Val1 Now Are:" VAL3.WIDTH.TOSTRING () "....." val3.height.tostring ()); console.writeline (bpass.tostring ()); console.readline ();}} When the reference type variable When transmitting, change the first variable, corresponding to the address content in the heap, and two variables in front and rear references are simultaneously changed simultaneously, and equal. When the value of the value is passed, it is redistributed an address in the stack, that is, the two variables correspond to different storage locations in memory, so when one change, the other is another value, no The change will change, so the two variables after the change are not equal. Beginning procedures, understanding this is not deep enough, please also criticize more!