1.ref指针传递参数来传递最大值和最小值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/**
1.第一种方法: 用ref 指针来传递参数
*/
namespace ConsoleApplication3
{
class Program
{
private int max;
private int min;
static void Main(string[] args)
{
Program p = new Program();
int max1=0;
int min1=0;
int[] array = new int[5] { 1, 2, 3, 4, 5 };
p.getMaxandMin(array, ref max1, ref min1);
Console.WriteLine("最大值为{0},最小值为{1}",max1, min1);
Console.Read();
}
public void getMaxandMin(int[] array, ref int max,ref int min)
{
max = array[0];
min = array[0];
for (int i = 0; i < array.Length; i++)
{
if (array[i] > max)
{
max = array[i];
}
if (array[i] < min)
{
min = array[i];
}
}
}
}
}
2.传入一个大小为2的一位数组来传递最大值和最小值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
int maxa = 0,mina =0;
int []array = new int[5]{1,2,3,4,5};
int[] array2 = new int[2];
p.getMaxandMin(array, array2);
Console.WriteLine("最大值为" + array2[0]);
Console.WriteLine("最小值为" + array2[1]);
Console.Read();
}
public void getMaxandMin(int[] array,int []array2)
{
int max = array[0];
int min= array[0];
for (int i = 0; i < array.Length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
array2[0] = max;
array2[1] = min;
}
}
}
3.传入一个对象来传递数组的最大值和最小值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
private int max;
private int min;
static void Main(string[] args)
{
Program p = new Program();
p.max = 0;
p.min = 0;
int []array = new int[5]{1,2,3,4,5};
p.getMaxandMin(array, p);
Console.WriteLine("最大值为{0},最小值为{1}", p.max,p.min);
Console.Read();
}
public void getMaxandMin(int[] array,Program p2)
{
int max = array[0];
int min= array[0];
for (int i = 0; i < array.Length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
p2.max = max;
p2.min = min;
}
}
}