博客
关于我
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/

你可能感兴趣的文章
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>