关于委托、indexof、substring的一个例子

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("Enter 2 numbers separated with a comma:"); //输入两个数字并且以逗号隔开
            string input = Console.ReadLine();
            int commaPos = input.IndexOf(',');//indexof:查找第一次出现逗号的位置;indexof(','2)第二次出现逗号的位置;indexof(',',3,2)从第三开始查,查两位;返回值是绝对位置,为空则为 -1
            double param1 = Convert.ToDouble(input.Substring(0, commaPos));//Substring:截取从0-commaPos(2)的字符串
            double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
            Console.WriteLine("Enter M to Multiply or D to divide:");
            input = Console.ReadLine();
            if (input == "M")
                process = new ProcessDelegate(Multiply);
            else
                process = new ProcessDelegate(Divide);
            Console.WriteLine("Result: {0}", process(param1, param2));
            Console.ReadKey();
            }

暧昧帖

Tags :

Google Reader Yahoo Facebook Twitter Digg FriendFeed Delicious Google Translate
This entry was posted onJuly 9th, 2009 at 11:32. You can follow any responses to this entry through the RSS 2.0. You can Leave a response, or Trackback.

One Response

Comments(1)Trackbacks(0)

  1. onyx

    интересно читать, спасибо

    2009/09/01 22:07 | #1 @

Leave a Reply

(Ctrl+Enter)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

27 queries. 0.210 seconds