【05】C#特有的ref、out参数

java和C#非常相似,它们大部分的语法是一样的,但尽管如此,也有一些地方是不同的。

为了更好地学习java或C#,有必要分清它们两者到底在哪里不同。

我们这次要来探讨C#特有的ref、out参数

 

java代码:

 1 public class HelloWorld {
 2     public static int n1=10;
 3     public static int n2=20;
 4     public static void main(String[] args) {
 5         System.out.println("n1:"+n1);   //10
 6         System.out.println("n2:"+n2);   //20
 7         Test();
 8         System.out.println("----交换后----");
 9         System.out.println("n1:"+n1);   //20
10         System.out.println("n2:"+n2);   //10
11     }
12 
13     private static void Test() {
14         int temp=n1;
15         n1=n2;
16         n2=temp;
17     }
18 }

 

C#代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HelloWorld
 8 {
 9     class Program
10     {
11         public static int n1 = 10;
12         public static int n2 = 20;
13         static void Main(string[] args)
14         {
15             Console.WriteLine("n1:" + n1);  //10
16             Console.WriteLine("n2:" + n2);  //20
17             Console.WriteLine("----交换后----");
18             Test();
19             Console.WriteLine("n1:" + n1);  //20
20             Console.WriteLine("n2:" + n2);  //10
21             Console.ReadKey();
22         }
23 
24         private static void Test()
25         {
26             int temp = n1;
27             n1 = n2;
28             n2 = temp;
29         }
30     }
31 }

 

C#还可以使用ref函数来实现:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HelloWorld
 8 {
 9     class Program
10     {
11 
12         static void Main(string[] args)
13         {
14             int n1 = 10;
15             int n2 = 20;
16             Console.WriteLine("n1:" + n1);  //10
17             Console.WriteLine("n2:" + n2);  //20
18             Console.WriteLine("----交换后----");
19             Test(ref n1, ref n2);
20             Console.WriteLine("n1:" + n1);  //20
21             Console.WriteLine("n2:" + n2);  //10
22             Console.ReadKey();
23         }
24 
25         private static void Test(ref int n1, ref int n2)
26         {
27             int temp = n1;
28             n1 = n2;
29             n2 = temp;
30         }
31     }
32 }

 

案例获得最大最小值:

java代码:

 1 public class HelloWorld {
 2 
 3     public static void main(String[] args) {
 4         int[] nums = {1, 2, 3, 4};
 5         int[] res = GetMaxMin(nums);
 6         System.out.println(String.format("最大值是:%d 最小值为:%d", res[0], res[1]));
 7     }
 8 
 9     private static int[] GetMaxMin(int[] nums) {
10         int[] res = new int[4];
11         //设res[0]为最大值,res[1]为最小值
12         res[0] = nums[0];
13         res[1] = nums[0];
14         for (int i = 0; i < nums.length; i++)
15         {
16             if(nums[i]>res[0])
17             {
18                 res[0]=nums[i];
19             }
20             if(nums[i]<res[1])
21             {
22                 res[1]=nums[i];
23             }
24         }
25         return res;
26 
27     }
28 
29 }

 

 C#代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HelloWorld
 8 {
 9     class Program
10     {
11 
12         static void Main(string[] args)
13         {
14             int[] nums = { 1, 2, 3, 4 };
15             int[] res = GetMaxMin(nums);
16             Console.WriteLine("最大值是:{0} 最小值为:{1}", res[0], res[1]);
17             Console.ReadKey();
18 
19         }
20 
21         private static int[] GetMaxMin(int[] nums)
22         {
23             int[] res = new int[4];
24             //设res[0]为最大值,res[1]为最小值
25             res[0] = nums[0];
26             res[1] = nums[0];
27             for (int i = 0; i < nums.Length; i++)
28             {
29                 if (nums[i] > res[0])
30                 {
31                     res[0] = nums[i];
32                 }
33                 if (nums[i] < res[1])
34                 {
35                     res[1] = nums[i];
36                 }
37             }
38             return res;
39         }
40     }
41 }

 

C#特有的out函数:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HelloWorld
 8 {
 9     class Program
10     {
11 
12         static void Main(string[] args)
13         {
14             int[] nums = { 1, 2, 3, 4 };
15             int max;
16             int min;
17             Test(nums, out max, out min);
18             Console.WriteLine("最大值是:{0} 最小值为:{1}",max, min);
19             Console.ReadKey();
20 
21         }
22 
23         private static void Test(int[] nums, out int max, out int min)
24         {
25             max = nums[0];
26             min = nums[0];
27             for (int i = 0; i < nums.Length; i++)
28             {
29                 if (nums[i] > max)
30                 {
31                     max = nums[i];
32                 }
33                 if (nums[i] < min)
34                 {
35                     min = nums[i];
36                 }
37             }
38         }
39     }
40 }

 

out参数可以返回不同类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {

        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4 };
            int max;
            int min;
            double avg; //特有:不同类型
            Test(nums, out max, out min, out avg);
            Console.WriteLine("最大值是:{0} 最小值为:{1} 平均值为:{2}", max, min, avg);
            Console.ReadKey();

        }

        private static void Test(int[] nums, out int max, out int min, out double avg)
        {
            max = nums[0];
            min = nums[0];
            int sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
                if (nums[i] < min)
                {
                    min = nums[i];
                }
                sum += nums[i];
            }
            avg = 1.0 * sum / nums.Length;
        }
    }
}

 

分析和总结:

1、我们在Main()函数中,调用Test()函数,我们管Main()函数称之为调用者,管Test()函数称之为被调用者。

* 如果被调用者想要得到调用者的值:
1)、传递参数。
2)、使用静态字段来模拟全局变量。
如果调用者想要得到被调用者的值:
1)、返回值

2、不管是实参还是形参,都是在内存中开辟了空间的。

3、方法的功能一定要单一。
GetMax(int n1,int n2)
方法中最忌讳的就是出现提示用户输入的字眼。

4、C#特有的out、ref

值类型的参数,传递的是一份复制。形参的改变不会影响到外面的值。但是c#提供两个关键字 ref和out 让我们可以把值类型当成引用类型来传递。

java不支持ref和out这两个关键字 并且传递的值类似也是一份拷贝。所以改变不会影响外面的值。

1)、out参数。
如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组。
但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,我们可以考虑使用out参数。
out参数就侧重于在一个方法中可以返回多个不同类型的值。

2)、ref参数
能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。ref参数要求在方法外必须为其赋值,而方法内可以不赋值。

 

 

 

 

谢谢观看!

 

posted @ 2019-12-09 10:31  the_path_of_grace  阅读(444)  评论(0编辑  收藏  举报