博客
关于我
out 参数
阅读量:250 次
发布时间:2019-03-01

本文共 2825 字,大约阅读时间需要 9 分钟。

返回多个 相同 类型,可以考虑返回一个数组

返回多个 不同 类型,out 参数 闪亮登场;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _07out参数{       class Program    {           static void Main(string[] args)        {               //写一个方法 求一个数组中的最大值、最小值、总和、平均值            int[] numbers = {    1, 2, 3, 4, 5, 6, 7, 8, 9 };            将要返回的4个值,放到一个数组中返回            //int[] res = GetMaxMinSumAvg(numbers);            //Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均值是{3}", res[0], res[1], res[2], res[3]);            //Console.ReadKey();            int max1;            int min1;            int sum1;            int avg1;            bool b;            string s;            double d;            Test(numbers, out max1, out min1, out sum1, out avg1, out b, out s, out d);            Console.WriteLine(max1);            Console.WriteLine(min1);            Console.WriteLine(sum1);            Console.WriteLine(avg1);            Console.WriteLine(b);            Console.WriteLine(s);            Console.WriteLine(d);            Console.ReadKey();        }        ///         /// 计算一个数组的最大值、最小值、总和、平均值        ///         ///         /// 
public static int[] GetMaxMinSumAvg(int[] nums) { int[] res = new int[4]; //假设 res[0] 最大值 res[1]最小值 res[2]总和 res[3]平均值 res[0] = nums[0];//max res[1] = nums[0];//min res[2] = 0;//sum string name = "孙全"; bool b = true; for (int i = 0; i < nums.Length; i++) { //如果当前循环到的元素比我假定的最大值还大 if (nums[i] > res[0]) { //将当前循环到的元素赋值给我的最大值 res[0] = nums[i]; } if (nums[i] < res[1]) { res[1] = nums[i]; } res[2] += nums[i]; } //平均值 res[3] = res[2] / nums.Length; return res; } /// /// 计算一个整数数组的最大值、最小值、平均值、总和 /// /// 要求值得数组 /// 多余返回的最大值 /// 多余返回的最小值 /// 多余返回的总和 /// 多余返回的平均值 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) { //out参数要求在方法的内部必须为其赋值 max = nums[0]; min = nums[0]; 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 = sum / nums.Length; b = true; s = "123"; d = 3.13; } }}

转载地址:http://idux.baihongyu.com/

你可能感兴趣的文章
Mysql之索引选择及优化
查看>>
mysql之联合查询UNION
查看>>
mysql之连接查询,多表连接
查看>>
mysql乐观锁总结和实践 - 青葱岁月 - ITeye博客
查看>>
mysql也能注册到eureka_SpringCloud如何向Eureka中进行注册微服务-百度经验
查看>>
mysql乱码
查看>>
Mysql事务。开启事务、脏读、不可重复读、幻读、隔离级别
查看>>
MySQL事务与锁详解
查看>>
MySQL事务原理以及MVCC详解
查看>>
MySQL事务及其特性与锁机制
查看>>
mysql事务理解
查看>>
MySQL事务详解结合MVCC机制的理解
查看>>
MySQL事务隔离级别:读未提交、读已提交、可重复读和串行
查看>>
MySQL事务隔离级别:读未提交、读已提交、可重复读和串行
查看>>
webpack css文件处理
查看>>
mysql二进制包安装和遇到的问题
查看>>
MySql二进制日志的应用及恢復
查看>>
mysql互换表中两列数据方法
查看>>
mysql五补充部分:SQL逻辑查询语句执行顺序
查看>>
mysql交互式连接&非交互式连接
查看>>