Parameters through the value and reference delivery parameters in C #

xiaoxiao2021-03-20  244

In C #, you can pass values ​​or pass parameters by reference. The value of the parameter is allowed to change the value of the parameter by reference to the transfer parameters (Method, Properties, Indexer, Operators, and Constructor, and Constructor, and Constructor, and Constructor). To deliver parameters by reference, use the REF or OUT keyword. For simplicity, only the REF keyword is used in the example of this topic. For information on the differences between REF and OUT, see, use REF and OUT to pass an array.

This topic includes the following chapter:

Transfer value type parameter pass reference type parameters

It also includes the following example:

Example Demonstration Whether to use REF or OUT1 Value Password Cyrofin 2 By reference transfer value type is 3 swap value type (two integers) Yes 4 passing the value passing the reference type 5 Switch reference type (two A string)

Transfer value type parameters

The value type variable directly contains its data, which is different from the reference type variable, which contains references to its data. Therefore, transmitting the value type variable to the method means that a copy of the variable is transmitted to the method. There is no impact on the original data stored in the variable in the method. If you want the method called to change the parameter value, you must pass this parameter using the REF or OUT keyword. For the sake of simplicity, the following example uses REF.

Example 1: Type of value passage

The following example demonstrates the value type type parameters through the value. Pass the value to pass the variable Myint to method Squareit. Any changes that occur within the method have no effect on the original value of the variable.

// passingParams1.cs Using system; class passingvalbyval {static void squareit (int x) // the parameter x is passed by value. // changes to x Will NOT AFFECT The Original Value of Myint. {X * = x; console. WriteLine ("The Value Inside The Method: {0}", x);} public static void main () {int myint = 5; console.writeline ("The value before calling the method: {0}", Myint); Squareit (Myint); // passing myint by value. Console.writeline ("The valueAfter calling the method: {0}", myint);}}

Output

The value before calling the method: 5the value inside the method: 25the value instry calling the method: 5

Code discussion

The variable Myint is a value type, contains its data (value 5). When tapping Squareit, the content of Myint is copied into the parameter X, and the parameter is scheduled to be square. But in main, myint's value is the same before calling the Squareit method. In fact, changes that occur within the method only affect the local variable x.

Example 2: Type by reference transmission value

The following example is the same as "Example 1" in addition to the use of the REF keyword. The value of the parameter changes after the call method.

// PassingParams2.cs using System; class PassingValByRef {static void SquareIt (ref int x) // The parameter x is passed by reference // Changes to x will affect the original value of myInt {x * = x;.. Console. WriteLine ("The Value Inside The Method: {0}", x);} public static void main () {int myint = 5; console.writeline ("The value before calling the method: {0}", Myint); Squareit (Ref Myint); // Passing Myint by Reference. Console.writeline ("The ValueAfter Calling The Method: {0}", Myint);}}

THE VALUE BEFORE CALLING: 5THE VALUE INSIDE The Method: 25The ValueAfter Calling The Method: 25

Code discussion

In this example, the passing is not a value of Myint, but a reference to myint. Parameter X is not an int type, it is a reference to INT (this example is a reference to myint). Therefore, when the square is square in the method, the square is actually begging is the item referenced by X: Myint.

Example 3: Switch value type

The common example of the value of the transfer parameter is the SWAP method, and the two variables of X and Y are delivered in this method, and then exchange their contents. Parameters must be delivered to the SWAP method by reference; otherwise, the processing will be the local copy of the parameters within the method. The following is an example of a SWAP method using reference parameters:

Static void swapByref (Ref INT X, REF INT Y) {INT TEMP = X; X = Y; Y = Temp;}

When calling this method, use the REF keyword in the call, as shown below:

SwapByref (Ref I, REF J);

Transfer reference type parameters

The reference type variable does not directly contain its data; it contains references to its data. When passing the parameters of the reference type by value, it is possible to change the data pointed to by the reference, such as the value of a certain member. However, it is impossible to change the value of the reference itself; that is, the same reference cannot be used to allocate memory and keep it outside the block. To do this, use the REF (or OUT) keyword delivery parameter. For the sake of simplicity, the following example uses REF.

Example 4: Transfer reference type by value

The following example demonstrates the parameter MyArray that passes the reference type to the Change method. Since this parameter is a reference to MyArray, it is possible to change the value of array elements. However, when trying to reallocate the parameters to a different memory location, the operation is only valid within the method and does not affect the original variable MyArray.

// PassingParams4.cs // Passing an array to a method without the ref keyword.// Compare the results to those of Example 5.using System; class PassingRefByVal {static void Change (int [] arr) {arr [0] = 888; // this change. Arr = new int [5] {-3, -1, -2, -3, -4}; // this change is local. Console.writeline ("Inside the Method , The first element is: {0} ", Arr [0]);} public static void main () {int [] myarray = {1,4,5}; console.writeline (" Inside Main, Before Calling the Method , The first element is: {0} ", MyArray [0]); Console.writeline (" INSIDE Main, After Calling the Method, The First Element IS: {0} ", MyArray [0]) }} Output

Inside Main, Before Calling The Method, The First Element IS: 1Inside The Method, The First Element Is: -3inside Main, After Calling The Method, The First Element Is: 888

Code discussion

In the previous example, the array MyArray is a reference type, and is passed to the method without using the REF parameter. In this case, a copy of the reference to MyArray will be passed to the method. The output display method is possible to change the content of the array element (from 1 to 888). However, use the New operator in the Change method to assign a new memory portion, which will cause the variable Arr to reference the new array. Therefore, any changes after this will not affect the original array MyArray (it is created in main). In fact, two arrays are created in this example, one in main, one in the CHANGE method.

Example 5: Type of reference by reference

This example is the same as "Example 4" in addition to the REF keyword in the method head and call. Any changes that occur within the method will affect the original variables in the call program.

// PassingParams5.cs // Passing an array to a method with the ref keyword.// Compare the results to those of Example 4.using System; class PassingRefByRef {static void Change (ref int [] arr) {// Both of The Following Changes Will Affect The Original Variables: Arr [0] = 888; Arr = New Int Int [5] {-3, -1, -2, -3, -4}; console.writeline ("INSIDE THE Method, The First Element is: {0} ", arr [0]);} public static void main () {int [] myarray = {1,4,5}; console.writeline (" Inside Main, Before Calling the Method, The Method, The First Element is: {0} ", MyArray [0]); Console.writeline (" Inside Main, After Calling The Method, The First Element IS: {0} ", MyArray [0]); }} Output

Inside Main, Before Calling The Method, The First Element IS: 1INSIDE The Method, The First Element is: -3inside Main, After Calling The Method, The First Element IS: -3

Code discussion

All changes that occur within the method affect the original array in the MAIN. In fact, the original array is reassigned using the New operator. Therefore, after calling the Change method, any reference to myArray will point to the array of five elements created in the CHANGE method.

Example 6: Switch two strings

The switching string is a good example of the pass reference type parameters. In this example, STR1 and STR2 two strings are initialized in main, and is passed to the swapstrings method as the parameters modified by the REF keyword. These two strings are exchanged within the method and in main.

// PassingParams6.csusing System; class SwappinStrings {static void SwapStrings (ref string s1, ref string s2) // The string parameter x is passed by reference // Any changes on parameters will affect the original variables {string temp = s1.. ; S1 = S2; S2 = Temp; console.writeline ("INSIDE The Method: {0}, {1}", S1, S2);} public static void main () {string str1 = "john"; string str2 = "Smith"; console.writeline ("INSIDE Main, Before Swapping: {0} {1}", STR1, STR2); SWAPSTRINGS (Ref str1, ref str2); // Passing strings by reference console.writeline ("INSIDE Main After swapping: {0}, {1} ", str1, str2);}} output

INSIDE Main, Before Swapping: John Smithinside The Method: Smith, Johninside Main, After Swapping: Smith, John

转载请注明原文地址:https://www.9cbs.com/read-130400.html

New Post(0)