大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
/* 20160324 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch06 { class Program { //params 参数数组 static int SumVals(params int[] vals) { int sum = 0; foreach (int val in vals) { sum += val; } return sum; } //值引用, 1.val is not an const value; 2. val must be initialized static void showDouble(ref int val) { val *= 2; return; } //6.4 struct function 结构函数 struct CustomerName { public string firstName, lastName; public string WriteName() { return firstName + " " + lastName; } } //6.5 函数重载 static int MaxValue(params int[] inArray) { int maxVal = inArray[0]; for (int i = 1; i < inArray.Length; i++) { if (inArray[i] > maxVal) maxVal = inArray[i]; } return maxVal; } static double MaxValue(params double[] inArray) { double maxVal = inArray[0]; for (int i = 1; i < inArray.Length; i++) { if(inArray[i]>maxVal) maxVal = inArray[i]; } return maxVal; } //6.6 delegate 委托 //1.委托的返回值和参数与函数的返回值和参数相同 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) { //params 参数数组,并不限定输入的参数个数 int sum = SumVals(1,5,2,4,5); Console.WriteLine("Summed Values = {0}, ",sum); sum = SumVals(2, 4, 6, 8, 10, 12, 14, 16); Console.WriteLine("Summed Values = {0}.", sum); //Main函数参数,带参数执行 //右击项目名称,选择属性,选择调试,添加命令行参数,运行 foreach(string arg in args) { Console.WriteLine("{0}",arg); } Console.WriteLine("{0} command line argument were specified.", args.Length); //ref 引用类型参数,函数内部改变参数值的方法 Console.WriteLine("Please enter a number."); int enterInt; enterInt = Convert.ToInt32(Console.ReadLine()); showDouble(ref enterInt); Console.WriteLine("double is {0}.", enterInt); //结构函数,结构中定义函数 CustomerName myCustomer; myCustomer.firstName = "John"; myCustomer.lastName = "Franklin"; Console.WriteLine(myCustomer.WriteName()); //函数的重载,相同函数名,不同的返回值类型和参数类型 double result = MaxValue(1, 2, 3, 4, 5.2); Console.WriteLine("max number {0}", result); //delegate 委托 //定义委托 ProcessDelegate process; Console.WriteLine("Enter 2 numbers separated with a comma:"); string inputString = Console.ReadLine(); int commaPos = inputString.IndexOf(','); double param1 = Convert.ToDouble(inputString.Substring(0, commaPos)); double param2 = Convert.ToDouble(inputString.Substring(commaPos + 1, inputString.Length - commaPos - 1)); Console.WriteLine("Enter M to mutiply or D to Divide:"); inputString = Console.ReadLine(); if (inputString == "M") process = new ProcessDelegate(Multiply);//将委托初始化为函数引用Multiply,可简写为process = Multiply; else process = new ProcessDelegate(Divide);//将委托初始化为函数引用Divide,可简写为processs = Divide; Console.WriteLine("Result:{0}", process(param1, param2));//使用委托变量调用它引用的函数 Console.ReadKey(); } } }
成都做网站、网站建设、外贸营销网站建设服务团队是一支充满着热情的团队,执着、敏锐、追求更好,是创新互联的标准与要求,同时竭诚为客户提供服务是我们的理念。创新互联把每个网站当做一个产品来开发,精雕细琢,追求一名工匠心中的细致,我们更用心!