C#关键字参数

1、ref参数

个人理解:将一个值用ref参数修饰,带入方法计算,然后将值返回,此时在方法中不需要写return来返回值,会自动返回计算好的值
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ref参数
 8 {
 9     internal class Program
10     {
11         static void Main(string[] args)
12         {
13             double s = 5000;
14             //打印的返回值为5000
15             //jisum(s);
16             //Console.WriteLine(s);
17 
18 
19             //打印的返回值为5200
20             jisum(ref s);
21             Console.WriteLine(s);
22             Console.ReadKey();
23         }
24 
25         public static void jisum(ref double s)
26         {
27             s += 200;
28         }
29     }
30 }

 

2、out参数

如果在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组,但是如果返回多个不同类型的值的时候,返回数组的方式就不可取了,这个时候可以考虑使用out参数
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 out参数
 8 {
 9     internal class Program
10     {
11         static void Main(string[] args)
12         {
13             int[]  numbers =  { 1, 2, 3, 4, 5, 6 };
14             int max;
15             string s;
16             char c;
17             //调用方法前必须传入所需要的实参
18             JiMax(numbers,out max,out s,out c);
19             Console.WriteLine(max);
20             Console.WriteLine(s);
21             Console.WriteLine(c);
22             Console.ReadKey();
23 
24         }
25 
26         /// <summary>
27         /// 在同一个方法中计算数组中最大值,并返回不同数据类型的参数
28         /// </summary>
29         /// <param name="numbers">数组</param>
30         /// <param name="max">最大值</param>
31         /// <param name="s">字符串类型值</param>
32         /// <param name="c">bool类型值</param>
33         public static void JiMax(int[] numbers,out int max,out string s, out char c)
34         {
35             ///离开当前方法之前必须对所有的out形参赋初始值
36             max = numbers[0];   
37             for (int i = 0; i < numbers.Length; i++)
38             {
39                 if (numbers[i] > max)
40                 {
41                     max = numbers[i];
42                 }
43             }
44 
45             s = "你好!";
46             c = '';
47 
48         }
49     }
50 }

 

3、params可变参数

将实参列表中跟可变参数数组类型一致的元素当做数组中的元素去处理,如果传入的实参类型与可变参数数据类型不一致则无法加入到可变参数数组中
可变参数必须是参数列表的最后一个元素
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace params参数
 8 {
 9     internal class Program
10     {
11         static void Main(string[] args)
12         {
13             Test("张三", 1, 2, 3);
14         }
15 
16 
17         //params必须是参数列表中的最后一个元素,将所有相同类型的实参添加到params修饰的形参数组中
18         public static void Test(string name,params int[] numbers)
19         {
20             int max = 0;
21             for (int i = 0; i < numbers.Length; i++)
22             {
23                 if (numbers[i]>max)
24                 {
25                     max = numbers[i];
26                 }
27             }
28             Console.WriteLine("最大值是{0}",max);
29 
30         }
31     }
32 }

 

posted @ 2022-06-08 15:30  hpc_for_s  阅读(89)  评论(0)    收藏  举报