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)
相关推荐
专注VB编程开发20年4 分钟前
javascript的类,ES6模块写法在VSCODE中智能提示
开发语言·javascript·vscode
Nemo_XP1 小时前
HttpHelper类处理两种HTTP POST请求
c#
Fireworkitte7 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083167 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT7 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.7 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超7 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice7 小时前
对象的finalization机制Test
java·开发语言·jvm
思则变8 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang8 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#