zoukankan      html  css  js  c++  java
  • 传递引用类型参数

    引用类型的变量不直接包含其数据;它包含的是对其数据的引用。当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值。但是无法更改引用本身的值;也就是说,不能使用相同的引用为新类分配内存并使之在块外保持。若要这样做,应使用 refout 关键字传递参数。为了简单起见,下面的示例使用 ref

    下面的示例演示通过值向 Change 方法传递引用类型的参数 arr。由于该参数是对 arr 的引用,所以有可能更改数组元素的值。但是,试图将参数重新分配到不同的内存位置时,该操作仅在方法内有效,并不影响原始变量 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

    在上个示例中,数组 arr 为引用类型,在未使用 ref 参数的情况下传递给方法。在此情况下,将向方法传递指向 arr 的引用的一个副本。输出显示方法有可能更改数组元素的内容,在这种情况下,从 1 改为 888。但是,在 Change 方法内使用 new 运算符来分配新的内存部分,将使变量 pArray 引用新的数组。因此,这之后的任何更改都不会影响原始数组 arr(它是在 Main 内创建的)。实际上,本示例中创建了两个数组,一个在 Main 内,一个在 Change 方法内。

    本示例除在方法头和调用中使用 ref 关键字以外,其余与上个示例相同。方法内发生的任何更改都会影响调用程序中的原始变量。

     
    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

    方法内发生的所有更改都影响 Main 中的原始数组。实际上,使用 new 运算符对原始数组进行了重新分配。因此,调用 Change 方法后,对 arr 的任何引用都将指向 Change 方法中创建的五个元素的数组。

    交换字符串是通过引用传递引用类型参数的很好的示例。本示例中,str1str2 两个字符串在 Main 中初始化,并作为由 ref 关键字修改的参数传递给 SwapStrings 方法。这两个字符串在该方法内以及 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);
        }
    }
    
    

    Inside Main, before swapping: John Smith

    Inside the method: Smith John

    Inside Main, after swapping: Smith John

    本示例中,需要通过引用传递参数以影响调用程序中的变量。如果同时从方法头和方法调用中移除 ref 关键字,则调用程序中不会发生任何更改。

  • 相关阅读:
    Codeforces 17.E Palisection
    poj4052 Hrinity
    bzoj2565 最长双回文串
    Java基础知识强化之集合框架笔记43:Set集合之TreeSet存储Integer类型的元素并遍历
    Java基础知识强化之集合框架笔记42:Set集合之LinkedHashSet的概述和使用
    Java基础知识强化之集合框架笔记41:Set集合之HashSet存储自定义对象并遍历练习
    Java基础知识强化之集合框架笔记40:Set集合之HashSet存储自定义对象并遍历
    TCP/IP协议原理与应用笔记13:底层网络技术之传输介质
    Java基础知识强化之集合框架笔记39:Set集合之HashSet存储字符串并遍历
    Java基础知识强化之集合框架笔记38:Set集合之Set集合概述和特点
  • 原文地址:https://www.cnblogs.com/philzhou/p/3156878.html
Copyright © 2011-2022 走看看