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

你可能感兴趣的文章
webpack loader配置全流程详解
查看>>
mysql主从复制,读写分离,半同步复制实现
查看>>
MySQL主从失败 错误Got fatal error 1236解决方法
查看>>
MySQL主从架构与读写分离实战
查看>>
MySQL主从篇:死磕主从复制中数据同步原理与优化
查看>>
mysql主从配置
查看>>
MySQL之2003-Can‘t connect to MySQL server on ‘localhost‘(10038)的解决办法
查看>>
MySQL之CRUD
查看>>
MySQL之DML
查看>>
Mysql之IN 和 Exists 用法
查看>>
MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法
查看>>
MySQL之SQL语句优化步骤
查看>>
MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
查看>>
Mysql之主从复制
查看>>