C# 方法的传参

引用传参

csharp 复制代码
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 关键字:

csharp 复制代码
t.change(ref a, ref b);

输出传参

csharp 复制代码
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;
        }
    }

}
csharp 复制代码
public int foo(int x, int y, out int a, out int b)

此方法返回一个intfoo,和两个out int

注意调用时要带out

csharp 复制代码
t.foo(2, 3, out a, out b)
相关推荐
皮皮林5511 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河1 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程4 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅6 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者7 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺7 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart8 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP9 小时前
MyBatis-mybatis入门与增删改查
java
孟陬12 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端