namespace Test
{
class Method
{
//值参数方法
public int method_1(int x,int y,int z)
//将对应的实参的值复制到形参中
{
int t;
//将x,y,z从大到小排序
if (x < y) { t = x; x = y; y = t; }
if (x < z) { t = x; x = z; z = t; }
if (y < z) { t = y; y = z; z = t; }
return x;
}
//引用参数方法
public void method_2( ref int x,ref int y,ref int z)
//在方法内对形参的修改就是对外部实参变量的修改
{
int t;
//将x,y,z从小到大排序
if (x > y) { t = x; x = y; y = t; }
if (x > z) { t = x; x = z; z = t; }
if (y > z) { t = y; y = z; z = t; }
}
//输出参数方法
public void method_3(int x, int y, int z,out
int max) //只能用于从方法中传出值,方法结束之前应该对out参数赋值
{
int t;
//将x,y,z从小到大排序
if (x > y) { t = x; x = y; y = t; }
if (x > z) { t = x; x = z; z = t; }
if (y > z) { t = y; y = z; z = t; }
max = z;
}
//参数数组方法
public void method_4(out int max, params int[] arr)
//使用params关键字来指定一个可变长的参数表
{
max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
}
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int[] array = { r.Next(100), r.Next(100), r.Next(100) };
label1.Text ="随机数1:"+ array[0].ToString()+ "\r\n随机数2:"+array[1].ToString() +"\r\n随机数3:"+ array[2].ToString();
int Max;
Method method = new Method();
Max = method.method_1(array[0], array[1], array[2]);
textBox1.Text = "值参数方法,最大值:"+Max.ToString()+"\r\n";
method.method_2(ref array[0],ref
array[1],ref array[2]);
textBox1.Text += "引用参数方法,最大值:" + array[2].ToString() + "\r\n";
method.method_3(array[0],array[1], array[2],out Max);
textBox1.Text += "输出参数方法,最大值:" + Max.ToString() + "\r\n";
method.method_4(out Max,array);
textBox1.Text += "数组参数,最大值:" + Max.ToString() + "\r\n";
}
}
}