C#在代码中演示值传递和引用传递的含义,区别

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

namespace _02值传递与引用传递
{
    class Program
    {

        static void Main()
        {
            Console.WriteLine("*****传递分四种情况:1、分别是值传递值类型,2、引用传递值类型,3、值传递引用类型,4、引用传递引用类型");
            Console.WriteLine("***************");
            Console.WriteLine("1、值传递值类型,");
            int n = 5;
            Console.WriteLine("调用值传递值前的n值: {0}", n);

            ZhiChuanzhi(n);
            Console.WriteLine("调用值传递值之后n值发生改变: {0}", n);
            Console.WriteLine("变量 n 为值类型,包含其数据(值为 5)。当调用 ZhiChuanzhi 时,n 的内容被复制到参数 x 中,在方法内将该参数求平方。但在 Main 中,n 的值在调用 ZhiChuanzhi 方法前后是相同的。实际上,方法内发生的更改只影响局部变量 x。");
            Console.WriteLine("***************");
            Console.WriteLine("2、引用传递值类型,");
            YinyChuanzhi(ref n);
            System.Console.WriteLine("调用引用传递值之后n值发生改变: {0}", n);
            Console.WriteLine("本示例中,传递的不是 n 的值,而是对 n 的引用。参数 x 不是 int 类型,它是对 int n 的引用。因此,当在方法内对 x 求平方时,实际被求平方的是 x 所引用的项:n。");

            Console.WriteLine("***************");
            Console.WriteLine("3、值传递引用类型,");
            int[] arr = { 1, 4, 5 };
            Console.WriteLine("在 Main 内, 调用该方法的前第一个元素是: {0}", arr[0]);

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

            Console.WriteLine("***************");
            Console.WriteLine("4、引用传递引用类型");
            int[] arra = { 1, 4, 5 };
            System.Console.WriteLine("在 Main 内, 调用该方法的之前第一个元素是: {0}", arr[0]);

            Changeto(ref arra);
            System.Console.WriteLine("在 Main 内, 调用该方法的之后第一个元素是: {0}", arr[0]);
            Console.WriteLine("方法内发生的所有更改都影响 Main 中的原始数组。实际上,使用 new 运算符对原始数组进行了重新分配。因此,调用 Change 方法后,对 arr 的任何引用都将指向 Changeto 方法中创建的五个元素的数组。");


            Console.ReadKey();
        }

        // 该参数的值是通过值传递的。
        // 更改不会影响x的原始值
        static void ZhiChuanzhi(int x)
        
        {
            x *= x;
            Console.WriteLine("在值传递传值方法的内部X进行自乘的结果: {0}", x);
        }

        // 该参数的值是通过引用传递值的。
        // 更改不会影响x的原始值
        static void YinyChuanzhi(ref int x)
        
        {
            x *= x;
            Console.WriteLine("在引用传递传值方法的内部X进行自乘的结果: {0}", x);
        }
        //值传递引用类型
        static void Change(int[] pArray)
        {
            pArray[0] = 888;  // 此项更改会影响原始值
            pArray = new int[5] { -3, -1, -2, -3, -4 };   // 这个更改是局部的.
            Console.WriteLine("在方法内部的第一个元素是: {0}", pArray[0]);
        }
        static void Changeto(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]);
        }
    }
}


转载于:https://my.oschina.net/qau/blog/506064

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值