---------------函数---------------
参数数组:可指定一个特定的参数,必须是最后一个参数,可使用个数不定的参数调用函数,用params关键字定义它们
例如:
static int SumVals(params int[] vals)
{
int sum = ;
foreach(int val in vals)
{
sum += val;
}
return sum;
}
调用SumVals: int sum = SumVals(1,2,5,7,8,12,34);
结果sum=69
引用参数:关键字ref,传递参数本身而非传递参数的值
例如:
static void ShowDouble(ref int val)
{
val *= ;
}
int i = ;
ShowDouble(ref i);
结果i=10
输出参数:关键字out,传出参数
例如:
1 static int MaxValue(int[] intArray,out int maxIndex)
2 {
3 int maxVal = intArray[0];
4 maxIndex = 0;
5 for (int i = 0; i < intArray.Length; i++)
6 {
7 if (intArray[i] > maxVal)
8 {
9 maxIndex = i;
10 maxVal = intArray[i];
11 }
12 }
13 return maxVal;
14 }
调用MaxValue:
int[] intArray = {,,,,,,};
int maxIndex,maxValue;
maxValue = MaxValue(intArray,out maxIndex)
结果maxIndex = 6
委托实例:
class Program
{
delegate double ProcessDelegate(double param1, double param2); static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1, double param2)
{
return param1 / param2;
} static void Main(string[] args)
{
ProcessDelegate process;
Console.WriteLine("2 number");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
double param1 = Convert.ToDouble(input.Substring(, commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos + , input.Length - commaPos - ));
Console.WriteLine("xxx");
input = Console.ReadLine();
if (input == "M")
process = new ProcessDelegate(Multiply);
else
process = new ProcessDelegate(Divide);
Console.WriteLine("Result: {0}", process(param1, param2));
Console.ReadKey();
}
}
---------------调试和错误处理---------------
断点可选择性触发,列表如下:
1)总是中断
2)在Hit Count等于多少次时中断
3)在Hit Count是某个数的倍数时中断
4)在Hit Count大于等于多少次时中断
try…catch…finally
try块:抛出异常
catch块:执行抛出异常后的操作
finally块:不论是否抛出异常都会执行的操作