你参照下面的做,有基本的思想。
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
double a = 1, b = 2, c = 3;
Console.WriteLine("Mean1:{0}", MeanTool.Mean1(a, b, c));
double x1 = 0, y1 = 0, z1 = 0; //ref参数在使用前必须先赋值
MeanTool.Mean2(a, b, c,ref x1,ref y1,ref z1);
Console.WriteLine("Mean2:{0} {1} {2}", x1, y1, z1);
double x2, y2, z2; //out参数在使用前不必赋值
MeanTool.Mean3(a, b, c, out x2, out y2, out z2);
Console.WriteLine("Mean3:{0} {1} {2}", x2, y2, z2);
double x3=0, y3=0, z3=0;
MeanTool.Mean4(ref x3, ref y3, ref z3, 100, 200, 300, 400, 500); //参数不定
Console.WriteLine("Mean4:{0} {1} {2}", x3, y3, z3);
Console.ReadKey();
}
}
class MeanTool
{
public static double Mean1(double a, double b, double c) //静态方法可以用类直接调用
{
return (a + b + c)/3;
}
public static void Mean2(double a, double b, double c,ref double x,ref double y,ref double z)
{
x = a;
y = b;
z = c;
}
public static void Mean3(double a, double b, double c, out double x, out double y, out double z)
{
x = a+1;
y = b+1;
z = c+1;
}
public static void Mean4(ref double x, ref double y, ref double z,params int[] a) //params,用于参数个数不确定,必须是方法的最后一个参数
{
x = a[0];
y = a[1];
z = a[2];
}
}
}
[此贴子已经被作者于2016-3-30 22:35编辑过]