C#入门经典(第五版)学习笔记(二)


---------------函数---------------
参数数组:可指定一个特定的参数,必须是最后一个参数,可使用个数不定的参数调用函数,用params关键字定义它们
例如:
static int SumVals(params int[] vals)
{
    int sum = 0;
    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 *= 2;
}
int i = 5;
ShowDouble(ref i);
结果i=10

输出参数:关键字out,传出参数
例如:
static int MaxValue(int[] intArray,out int maxIndex)
{
    int maxVal = intArray[0];
    maxIndex = 0;
    for (int i = 0; i < intArray.Length; i++)
    {
        if (intArray[i] > maxVal)
        {
            maxIndex = i;
            maxVal = intArray[i];
        }
    }
    return maxVal;
}
调用MaxValue:
int[] intArray = {1,2,5,7,8,12,34};
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(0, commaPos));
            double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
            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块:不论是否抛出异常都会执行的操作

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10663598/viewspace-1189490/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10663598/viewspace-1189490/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值