Passing Reference-Type Parameters (C# Programming Guide)


http://topic.csdn.net/t/20061212/17/5225542.html#r_achor



Passing Reference-Type Parameters (C# Programming Guide)

Visual Studio 2005

A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword. For simplicity, the following examples use ref .

The following example demonstrates passing a reference-type parameter, arr , by value, to a method, Change . Because the parameter is a reference to arr , it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside 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]);
    }
}

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 preceding example, the array, arr , which is a reference type, is passed to the method without the ref parameter. In such a case, a copy of the reference, which points to arr , is passed to the method. The output shows that it is possible for the method to change the contents of an array element, in this case from 1 to 888 . However, allocating a new portion of memory by using the new operator inside the Change method makes the variable pArray reference a new array. Thus, any changes after that will not affect the original array, arr , which is created inside Main . In fact, two arrays are created in this example, one inside Main and one inside the Change method.

This example is the same as the previous example, except for using the ref keyword in the method header and call. Any changes that take place 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 element is: {0}"


, arr[0]);

        Change(ref


 arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}"


, arr[0]);
    }
}

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 of the changes that take place inside the method affect the original array in Main . In fact, the original array is reallocated using the new operator. Thus, after calling the Change method, any reference to arr points to the five-element array, which is created in the Change method.

Swapping strings is a good example of passing reference-type parameters by reference. In the example, two strings, str1 and str2 , are initialized in Main and passed to the SwapStrings method as parameters modified by the ref keyword. The two strings are swapped inside the method and inside Main as well.

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);
    }
}

Inside Main, before swapping: John Smith

Inside the method: Smith John

Inside Main, after swapping: Smith John

In this example, the parameters need to be passed by reference to affect the variables in the calling program. If you remove the ref keyword from both the method header and the method call, no changes will take place in the calling program.

For more information about strings, see string .

Reference

Passing Parameters (C# Programming Guide)
Passing Arrays Using ref and out (C# Programming Guide)
ref (C# Reference)
Reference Types (C# Reference)

Concepts

C# Programming Guide




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Happy
{
    public class Element
    {
        public int Number = 10;
    }
    public class Test
    {
        public  void Change(Element s)
        {
            Element r = new Element();
            r.Number = 100;
            s = r;
        }
    }
    public class Time
    {
        public static void Main()
        {
            Happy.Element ey = new Happy.Element();
            ey.Number = 8;
            Console.WriteLine(ey.Number + "/n ");
            Test tt = new Test();
             tt.Change(ey);
            //Element r = new Element();
            //r.Number = 100;
            //ey = r;
            Console.WriteLine(ey.Number);
            Console.Read();
        }
    }

    //public class Test
    //{
    //    public static void Change(Element s)
    //    {
    //        s.Number = 100;
    //    }
    //}
}


http://topic.csdn.net/t/20061212/17/5225542.html#r_achor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值