C# 参考呼叫Call by Reference/传出参数Output parameter

使用return时 一次只能传回一个值
如果需要回传一个以上的值 那return就无法做到
参考呼叫Call by Reference/传出参数Output parameter 可以传出多个值

两者的差异在於 参考呼叫Call by Reference 变数需要设定初值
而 传出参数Output parameter 不用


参考呼叫Call by Reference

做个主控台 测试

namespace Call_by_Reference_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            a = 10;
            b = 20;
            Console.WriteLine("方法外 交换前\t a={0}  b={1}", a, b);

            CallRef(ref a, ref b);
            Console.WriteLine("离开方法後\t a={0}  b={1}", a, b);
            Console.ReadLine();
        }

        private static void CallRef(ref int x, ref int y)
        {
            Console.WriteLine("方法内 交换前\t x={0}  y={1}", x, y);
            
            int z;
            z = x;
            x = y;
            y = z;

            Console.WriteLine("方法内 交换後\t x={0}  y={1}", x, y);
        }
    }
}

结果如下
方法外 交换前 a=10 b=20
方法内 交换前 x=10 y=20
方法内 交换後 x=20 y=10
离开方法後 a=20 b=10


传出参数Output parameter

namespace Output_parameter_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;

            CallOut(out a, out b);

            Console.ReadLine();
        }

        private static void CallOut(out int x, out int y)
        {
            int z;
            x = 20;
            y = 30;
            Console.WriteLine("方法内 交换前\t x={0}  y={1}", x, y);

            z = x;
            x = y;
            y = z;
            Console.WriteLine("方法内 交换後\t x={0}  y={1}", x, y);
        }
    }
}

结果如下
方法内 交换前 x=20 y=30
方法内 交换後 x=30 y=20


<<:  如何在 SQL Server AOAG 设定环境之下, 套用修补程序 (patching)?

>>:  SEO营销

【Day20】建立计数器的Test,并提供测试使用的方法 ୧☉□☉୨!

上一篇我们把计数器Component完成了,这篇要来写这个计数器的测试了! 首先我们要先建立一个属於...

git - 2 ( push github、找插件、token、branch+merge、tag、stash )

1. git push - 推送数据库到 Github (clone + push) (1)建立新专...

Day11 K平均演算法(K-means clustering algorithm)

什麽是K平均演算法? 讲人话就是从所有资料当中乱数选择K个中心点,把个别资料依照最近的中心点分成K群...

个人背景

阅读这类管理性的书籍或分享,我常常觉得必须要先了解作者的背景,可能在理解作者的作法上会有比较深的体悟...

Day 26 Docker-Compose nginx + flask container with filebeat-another structure

Day 26 Docker-Compose nginx + flask container with...