传值调用与引用调用

博客介绍了传值调用与引用调用,参数类型分为 in、ref、out 三种,默认是 in。in 类型在子方法修改变量,主方法值不变;ref 类型子方法修改后,主方法值也改变;out 类型主方法对应变量无需初始化,还给出了代码示例。
ContractedBlock.gifExpandedBlockStart.gif传值调用与引用调用
 1None.gifusing System;
 2None.gif
 3None.gifclass MethodCall
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public static void Main() 
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
 8InBlock.gif         * 参数类型分为 in, ref, out 三种,默认为 in。
 9InBlock.gif         * in 类型在子方法中修改了对应变量后,主方法中的值不会发生改变。
10InBlock.gif         * ref 类型在子方法中修改了对应变量后,主方法中的值也会发生改变。
11InBlock.gif         * out 主方法中对应的变量不需要初始化。
12InBlock.gif         * 
13ExpandedSubBlockEnd.gif         */
        
14InBlock.gif        int a = 3, b = 4, c;
15InBlock.gif        Console.WriteLine("Before Method Call : a = {0}, b = {1}, c 未赋值", a, b);
16InBlock.gif        AMethod(a, ref b, out c);
17InBlock.gif        Console.WriteLine("After  Method Call : a = {0}, b = {1}, c = {2}", a, b, c);
18ExpandedSubBlockEnd.gif    }

19InBlock.gif
20InBlock.gif    public static void AMethod(int x, ref int y, out int z)
21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
22InBlock.gif        x = 7;
23InBlock.gif        y = 8;
24InBlock.gif        z = 9;
25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/geovindu/archive/2005/12/19/300521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值