out参数,ref参数,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 _07out参数
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             //写一个方法 求一个数组中的最大值、最小值、总和、平均值
 14             int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 15             ////将要返回的4个值,放到一个数组中返回
 16
 17             //int[] res = GetMaxMinSumAvg(numbers);
 18             //Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均值是{3}", res[0], res[1], res[2], res[3]);
 19             //Console.ReadKey();
 20             int max1;
 21             int min1;
 22             int sum1;
 23             int avg1;
 24             bool b;
 25             string s;
 26             double d;
 27             Test(numbers, out max1, out min1, out sum1, out avg1, out b, out s, out d);
 28
 29             Console.WriteLine(max1);
 30             Console.WriteLine(min1);
 31             Console.WriteLine(sum1);
 32             Console.WriteLine(avg1);
 33             Console.WriteLine(b);
 34             Console.WriteLine(s);
 35             Console.WriteLine(d);
 36             Console.ReadKey();
 37
 38         }
 39         /// <summary>
 40         /// 计算一个数组的最大值、最小值、总和、平均值
 41         /// </summary>
 42         /// <param name="nums"></param>
 43         /// <returns></returns>
 44         public static int[] GetMaxMinSumAvg(int[] nums)
 45         {
 46             int[] res = new int[4];
 47             //假设 res[0] 最大值  res[1]最小值  res[2]总和  res[3]平均值
 48             res[0] = nums[0];//max
 49             res[1] = nums[0];//min
 50             res[2] = 0;//sum
 51             string name = "孙全";
 52             bool b = true;
 53             for (int i = 0; i < nums.Length; i++)
 54             {
 55                 //如果当前循环到的元素比我假定的最大值还大
 56                 if (nums[i] > res[0])
 57                 {
 58                     //将当前循环到的元素赋值给我的最大值
 59                     res[0] = nums[i];
 60                 }
 61                 if (nums[i] < res[1])
 62                 {
 63                     res[1] = nums[i];
 64                 }
 65                 res[2] += nums[i];
 66             }
 67             //平均值
 68             res[3] = res[2] / nums.Length;
 69             return res;
 70
 71
 72         }
 73
 74
 75         /// <summary>
 76         /// 计算一个整数数组的最大值、最小值、平均值、总和
 77         /// </summary>
 78         /// <param name="nums">要求值得数组</param>
 79         /// <param name="max">多余返回的最大值</param>
 80         /// <param name="min">多余返回的最小值</param>
 81         /// <param name="sum">多余返回的总和</param>
 82         /// <param name="avg">多余返回的平均值</param>
 83         public static void Test(int[] nums, out int max, out int min, out int sum, out int avg, out bool b, out string s, out double d)
 84         {
 85             //out参数要求在方法的内部必须为其赋值
 86             max = nums[0];
 87             min = nums[0];
 88             sum = 0;
 89             for (int i = 0; i < nums.Length; i++)
 90             {
 91                 if (nums[i] > max)
 92                 {
 93                     max = nums[i];
 94                 }
 95                 if (nums[i] < min)
 96                 {
 97                     min = nums[i];
 98                 }
 99                 sum += nums[i];
100             }
101             avg = sum / nums.Length;
102
103
104             b = true;
105             s = "123";
106             d = 3.13;
107         }
108
109
110
111     }
112 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _10ref参数
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             double salary = 5000;
14             JiangJin(ref salary);
15             Console.WriteLine(salary);
16             Console.ReadKey();
17         }
18
19         public static void JiangJin(ref double s)
20         {
21             s += 500;
22
23         }
24
25         public static void FaKuan(double s)
26         {
27             s -= 500;
28         }
29     }
30 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace _12params可变参数
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //   int[] s = { 99, 88, 77 };
14             //Test("张三",99,100,100,100);
15             //Console.ReadKey();
16
17             //求任意长度数组的和 整数类型的
18             int[] nums = { 1, 2, 3, 4, 5 };
19             int sum = GetSum(8,9);
20             Console.WriteLine(sum);
21             Console.ReadKey();
22         }
23
24         public static int GetSum(params int[] n)
25         {
26             int sum = 0;
27             for (int i = 0; i < n.Length; i++)
28             {
29                 sum += n[i];
30             }
31             return sum;
32         }
33
34
35         public static void Test(string name, int id, params int[] score)
36         {
37             int sum = 0;
38
39             for (int i = 0; i < score.Length; i++)
40             {
41                 sum += score[i];
42             }
43             Console.WriteLine("{0}这次考试的总成绩是{1},学号是{2}", name, sum, id);
44         }
45     }
46 }
时间: 2024-08-26 10:52:21

out参数,ref参数,params参数的相关文章

C#方法的参数 Ref Out Params 4种类型的参数

转载:http://www.cnblogs.com/kissdodog/archive/2013/05/11/3072815.html 一.按值传递参数 值参数是通过将实参的值复制到形参,来实现按值传递到方法,也就是我们通常说的按值传递. 方法被调用时,CLR做如下操作: 1.在托管堆栈中为形参分配空间: 2.将实参的值复制到形参. 这个太常用了,按值传递参数,是复制一份,因此不影响原来参数的值. public class Program { static void Main(string[]

C# 中out,ref,params参数的使用

C#中有三个高级参数,分别是out,ref,params: 1.out参数 方法使用return 只能返回一个值(一个数值或一个指针值),out参数可以帮助我们在一个方法中返回多个值,不限类型. 在使用out参数的时候需要注意: 在调用方法之前,对out参数传递的变量只需声明,可以赋值也可以不赋值,赋值也会在方法中被覆盖掉. 方法使用了out参数传递变量时,就是要返回值,所以必须在方法内为其赋值. 方法的参数使用out关键字修饰时,在调用该方法时,也要在out参数传递的变量前面加上out关键字才

C# 方法的out、ref、params参数

一.out参数实例 [实例]求一个数组中的最大值.最小值.总和.平均值 class Program { static void Main(string[] args) { //写一个方法 求一个数组中的最大值.最小值.总和.平均值 int[] nums = { 1, 2, 3, 4, 5, 6 ,7}; int max; int sum; int min; int avg; bool b; string s; double d; GetValue(nums, out max, out min,

传递参数ref与输出参数out

ref与out都可以在全局中改变传入的参数的值 但使用ref时,传入的参数必须已赋值,否则是非法的使用out时,传入参数可以未赋值,但无论如何都必须在方法中赋值 例: 1 static double a = 10; 2 static double b = 20; 3 4 static void Test(ref double x,out double y) 5 { 6 y = 0; //必须要在方法中赋值一下 7 x *= 10; 8 y *= 20; 9 Console.WriteLine(x

out参数,ref参数,params参数数组

params参数数组 params关键字可以为方法指定数目可变的参数.params关键字修饰的参数,可以传入任意数目的同类型参数,甚至可以不传入参数. 不过params修饰的参数必须是方法的最后一个参数,并且一个方法只能有一个params修饰的参数. 示例 public class MyClass { public static void UseParams(params int[] list) { for (int i = 0; i < list.Length; i++) { Console.

c#中的ref、out、params参数

out参数 与c++的引用的对比 out参数可以用来传递方法返回值,与c++中的引用有点像,但是还有有些不同: - 调用方法的时候必须写out参数 - 调用方法之前必须先分配空间 - 调用方法之前不用先赋值. - 必须在方法内部对out参数赋值; 下面自己实现一个tryparse函数 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threadin

C# 参考之方法参数关键字:params、ref及out

如果在为方法声明参数时未使用 ref 或 out,则该参数可以具有关联的值.可以在方法中更改该值,但当控制传递回调用过程时,不会保留更改的值.通过使用方法参数关键字,可以更改这种行为. params params 关键字可以指定在参数数目可变处采用参数的方法参数. 在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字. 示例: 字面意思比较难懂,所以看示例很有用. using System; class App { public stati

问题:C# params类型参数;结果:C#的参数类型:params、out和ref

C#的参数类型:params.out和ref PS:由于水平有限,难免会有错误和遗漏,欢迎各位看官批评和指正,谢谢~ 首先回顾一下C#声明一个方法的语法和各项元素,[]代表可选 [访问修饰符] 返回值 方法名([参数类型] 数据类型 参数名) {方法体} 这篇文章主要是为了讲解 参数表里面的可选项[参数类型],这个参数类型的设置会影响到我们对参数施加操作后的结果或影响我们对方法的调用方式. C#之中参数类型分为4种: 无:默认的,普通参数 params:不定长参数 out:输出变量 ref:引用

C#的参数类型:params、out和ref

parmas类型的参数 1 using System; 2 public class MyClass 3 { 4 public static void UseParams(params int[] list) 5 { 6 for (int i = 0 ; i < list.Length; i++) 7 { 8 Console.WriteLine(list[i]); 9 } 10 Console.WriteLine(); 11 } 12 public static void UseParams2(

浅谈c#的三个高级参数ref out 和Params

c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和Params,在这里我们来分别的讲解一下,在这里的我们先不做具体的解释,我会通过几个例子来做分别的解释. 一:out参数  1.首先我先给大家一个题:我们来写一个方法,来求一个数组中的最大值,最小值,总和,平均值.代码如下:  int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  通过分析我们会发现如果我们写一个方