C# 方法的传参

本文通过C#代码实例,介绍了如何在类的方法中使用ref关键字传递参数以及out参数的使用,展示了如何在程序中进行有效的数据交换和函数返回值处理。
摘要由CSDN通过智能技术生成

引用传参

using System;


public class Program
{
	public static void Main()
	{
		Tools t = new Tools();
		t.bf();
		double a = 10;
		double b = 20;
		t.change(ref a, ref b);
		Console.WriteLine(b);
	}
}

class Tools {
	public void af() {Console.Write("a\n");}
	internal void bf() {Console.Write("b\n");}
	public void change(ref double a, ref double b) {
		dynamic t;
		t = a;
		a = b;
		b = t;
	}
	
}

调用方法时,你必须使用 ref 关键字:

t.change(ref a, ref b);

输出传参

using System;

namespace LHJ {

    class TODO {
        static void Main() {
            Tools t = new Tools();

            int a, b;
            Console.WriteLine("add = {0}, doublea = {1}, double b = {2}",
                t.foo(2, 3, out a, out b), a, b);

        }
    }

    class Tools {
        public int foo(int x, int y, out int a, out int b) {
            a = x * 2;
            b = y * 2;
            return x + y;
        }
    }

}

public int foo(int x, int y, out int a, out int b)

此方法返回一个intfoo,和两个out int
注意调用时要带out

t.foo(2, 3, out a, out b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值