Pass reference type parameters (C # programming guide)

Source: Internet
Author: User

Variables of the reference type do not directly contain their data; they contain references to their data. When a parameter of the reference type is passed through a value, it is possible to change the data pointed to by the reference, such as the value of a certain type of member. However, you cannot change the reference value. That is, you cannot use the same reference to allocate memory for the new class and keep it out of the block. To do this, use the ref or out keyword to pass the parameter. For simplicity, the following example uses ref.

Example

The following example shows how to pass the arr parameter of the reference type to the Change method by using a value. Because this parameter is a reference to arr, it is possible to change the value of the array element. However, when you try to allocate parameters to different memory locations, this operation is only valid in the method and does not affect the original variable arr.

Class PassingRefByVal
{
Static void Change (int [] pArray)
{
PArray [0] = 888; // This change affects the original element.
PArray = new int [5] {-3,-1,-2,-3,-4}; // This change is local.
System. Console. WriteLine ("Inside the method, the first element is: {0}", pArray [0]);
}

Static void Main ()
{
Int [] arr = {1, 4, 5 };
System. Console. WriteLine ("Inside Main, before calling the method, the first element is: {0}", arr [0]);

Change (arr );
System. Console. WriteLine ("Inside Main, after calling the method, the first element is: {0}", arr [0]);
}
}
/* Output:
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is:-3
Inside Main, after calling the method, the first element is: 888
*/
In the previous example, the array arr is of the reference type and is not usedRefParameter. In this case, a copy of the reference pointing to arr is passed to the method. The output display method may change the content of the array element from 1 to 888. However, use new in the Change Method
The variable pArray references the new array. Therefore, any subsequent changes do not affect the original array arr (it is created in Main ). In fact, two arrays are created in this example, one in Main and the other in the Change method.

 

 

In this example, except that the ref keyword is used in the method header and call, it is the same as the previous example. Any changes made in the method will affect the original variables in the calling program.

Class PassingRefByRef {static void Change (ref int [] pArray) {// Both of the following changes will affect the original variables: pArray [0] = 888; pArray = new int [5] {-3,-1,-2,-3,-4}; System. console. writeLine ("Inside the method, the first element is: {0}", pArray [0]);} static void Main () {int [] arr = {1, 4, 5}; System. console. writeLine ("Inside Main, before calling the method, the first ele Ment is: {0} ", arr [0]); Change (ref arr); System. console. writeLine ("Inside Main, after calling the method, the first element is: {0}", arr [0]) ;}/ * Output: Inside Main, before calling the method, the first element is: 1 Inside the method, the first element is:-3 Inside Main, after calling the method, the first element is: -3 */all changes made in the method affect the original array in Main. In fact, the original array is reallocated using the new operator. Therefore, after the Change method is called, any reference to arr will point to the array of the five elements created in the Change method.

 

 

The exchange string is a good example of passing a reference type parameter through reference. In this example, the str1 and str2 strings are initialized in Main and passed to the SwapStrings method as parameters modified by the ref keyword. The two strings are exchanged in this method and in Main.

Class SwappingStrings {static void SwapStrings (ref string s1, ref string s2) // The string parameter is passed by reference. // Any changes on parameters will affect the original variables. {string temp = s1; s1 = s2; s2 = temp; System. console. writeLine ("Inside the method: {0} {1}", s1, s2);} static void Main () {string str1 = "John "; string str2 = "Smith"; System. console. writeLine ("Inside Main, Before swapping: {0} {1} ", str1, str2); SwapStrings (ref str1, ref str2); // Passing strings by reference System. console. writeLine ("Inside Main, after swapping: {0} {1}", str1, str2) ;}/ * Output: Inside Main, before swapping: John Smith Inside the method: smith John Inside Main, after swapping: Smith John */In this example, parameters must be passed through references to affect variables in the calling program. If the ref keyword is removed from both the method header and method call, no changes will be made to the calling program.
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.