C#out参数的简单实例

//在一个方法中,返回多个不同类型的返回值
//在调用前,out参数只定义不赋值,在方法中,必须为out参数赋值

static void Main(string[] args)
{

int[] num = { 1, 2, 3, 4, 5 };
int max;
int min;

double avg;

GetMaxMinAvg(num, out max, out min, out avg);
Console.WriteLine("{0},max是{1},min是{2},avg是{3}", num, max, min, avg);
Console.ReadKey();

}

public static void GetMaxMinAvg(int[] num, out int max, out int min, out double avg)
{

min = num[0];
max = num[0];
int sum = 0;

for (int i = 0; i < num.Length; i++)
{


    if (max < num[i])
    {

        max = num[i];
    
    }
    if (min > num[i])
    {

        min = num[i];
    
    
    }
    sum += num[i];



}

avg = (double)sum / num.Length;

}

posted @ 2024-12-01 18:20  菜鸟的奋斗军  阅读(1)  评论(0编辑  收藏  举报