Chapter 7 Arrays 253 7.6 Passing Arrays by (Web hosting servers)
Chapter 7 Arrays 253 7.6 Passing Arrays by Value and by Reference In C#, a variable that stores an object, such as an array, does not actually store the object itself. Instead, such a variable stores a reference to the object (i.e., the location in the computer s memory where the object itself is stored). The distinction between reference variables and primitive data type variables raises some subtle issues that programmers must understand to create secure, stable programs. When a program passes an argument to a method, the called method receives a copy of that argument s value. Changes to the local copy do not affect the original variable that the program passed to the method. If the argument is of a reference type, the method makes a local copy of the reference itself, not a copy of the actual object to which the reference refers. The local copy of the reference also refers to the original object in memory. Thus, reference types are always passed by reference, which means that changes to those objects in called methods affect the original objects in memory. Performance Tip 7.1 Passing arrays and other objects by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would waste time and would consume considerable storage for the copies of the arrays both of these problems cause poor performance. C# also allows methods to pass references with keyword ref. This is a subtle capability, which, if misused, can lead to problems. For instance, when a reference-type object like an array is passed with ref, the called method actually gains control over the passed reference itself, allowing the called method to replace the original reference in the caller with a different object or even with null. Such behavior can lead to unpredictable effects, which can be disastrous in mission-critical applications. The program in Fig. 7.9 demonstrates the subtle difference between passing a reference by value and passing a reference with keyword ref. Lines 26 and 29 declare two integer array variables, firstArray and firstArray- Copy (we make the copy so we can determine whether reference firstArray gets overwritten). Line 26 initializes firstArray with the values 1, 2 and 3. The assignment statement on line 29 copies reference firstArray to variable firstArrayCopy, causing these variables to reference the same array object in memory. The for structure on lines 38 39 prints the contents of firstArray before it is passed to method First- Double (line 42) so we can verify that this array is passed by reference (i.e., the called method indeed changes the array s contents). The for structure in method FirstDouble (lines 99 100) multiplies the values of all the elements in the array by 2. Line 103 allocates a new array containing the values 11, 12 and 13; the reference for this array then is assigned to parameter array (in an attempt to overwrite reference firstArray this, of course, will not happen, because the reference was passed by value). After method FirstDouble executes, the for structure on lines 48 49 prints the contents of firstArray, demonstrating that the values of the elements have been changed by the method (and confirming that in C# arrays are always passed by reference). The if/else structure on lines 52 57 uses the == operator to compare references firstArray (which we just attempted to overwrite) and firstArrayCopy. The expression on line 40 evaluates to true if the operands to binary operator == indeed reference the same object. In this case, the object represented is the array allocated in line 26 not the array allocated in method FirstDouble (line 103).
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.