方法参数传递形式

 值传递  引用传递 输出传递

static class Program

{

        //值传递调用

        int x = 20;

        int y = 30;                                                       // 定义

        TestMethod10(x, y);                                     // 传值

        Console.WriteLine("x y调换后{0},{1}", x, y);

        int q = 200;

        int w = 300;

        Testmethod11(ref q,ref w);

        console.WriteLine("x,y调换后{0},{1},q,w")

        int c ;

        bool isTrue = TestMethods12(out c)

        Console.WriteLine("retrun结果:{0},输出参数结果:{1}",isTrue,c)

        int d;

        int e;

        bool isXiaoHu = TestMothod13(10,20,out d, out e)

        Console.WriteLine("retrun结果{0},输出参数结果d:{1},输出参数结果e:{2}",isXiangHu,d,e);

}

1.值传递

特点  1.参数传递的默认方式

         2.当调用一个方法时,会为每个参数创建一个新的存储位置

         3.当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全

public static viod TestMethod10(int a, int b)

{

        int temp;

        temp = a;

        a = b;

        b = temp;

        Console.WriteLine("a和b调换后的值{0},{1}", a, b);

}

2.引用传递

1.引用参数是一个对变量的内存位置的引用 不会创建一个新的存储位置

2. 参数关键字 ref

public static void TestMethod11(ref int a, ref int b)

{

        int temp;

        temp = a;

        a = b;

        b = temp;

        Console.WriteLine("a和b调换后的值{0},{1}", a, b);

}

3.输出参数

1.是对于返回方法的补充,  return 语句可用于 只从函数中返回一个值 输出参数可以返回多个值

2.关键字 out

3.其他方面 与引用参数类似

public static bool TestMethod12(out int a)

{

a = 2000;

return true;

}

2

public static bool TestMethod13(int x,int y,out int a,out int b)

{

        if(x==y)

{

        a =x;

        b=y;

        return true

}

else
            {
                a = y;
                b = x;
                return false;
            }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值