博客
关于我
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中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>