198 Methods Chapter 6 In Section 6.8, we (Domain and web hosting)

198 Methods Chapter 6 In Section 6.8, we discussed the difference between value types and reference types. At this point, the reader can understand one of the major differences between the two data types value-type variables are passed to methods by value, whereas reference-type variables are passed to methods by reference. What if the programmer would like to pass a value type by reference? To do this, C# provides the ref and out keywords. The ref keyword specifies that a value-type argument should be passed by reference, which enables the called method to modify the original variable. This keyword is used for variables that already have been initialized. The out keyword specifies an output parameter, which is an argument to which the called method will assign a value. Normally, when a method receives an uninitialized value, the compiler generates an error. Preceding the parameter with keyword out specifies that the called method will initialize the variable and prevents the compiler from generating an error message for the uninitialized variable. Figure 6.8 demonstrates using the ref and out keywords to manipulate integer values.2 This program contains three methods to calculate the square of an integer. The first method, SquareRef (lines 26 29), multiplies its argument x by itself and assigns the new value to x. SquareRef receives its argument as a refint, specifying that xis an integer that is passed by reference to the method. As a result, the assignment at line 28 modifies the original argument s value, rather than a copy of that value. The second method, SquareOut (lines 33 37), does the same thing, but initializes x to 6 on line 35. SquareOut receives its argument as an outint, which indicates that x is an integer variable that the caller passes to method SquareOut by reference and that SquareOutcan assign a new value to this variable. The final method, Square (lines 41 44), simply takes x as a value-type integer argument and squares its value. 1 // Fig. 6.8: RefOutTest.cs 2 // Demonstrating ref and out parameters. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class RefOutTest : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 // main entry point for application 18 [STAThread] 19 static void Main() 20 { 21 Application.Run( new RefOutTest() ); 22 } Fig. 6.8 Demonstrating ref and out parameters. (Part 1 of 3.) 2. In Chapter 7 we discuss passing reference-type arguments by value and by reference.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Leave a Reply