C#入门经典学习笔记 <chapter06 函数>
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#入门经典学习笔记 <chapter06 函数>相关的知识,希望对你有一定的参考价值。
/* 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; } 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); Console.ReadKey(); } } }
本文出自 “学习永无止境” 博客,请务必保留此出处http://lanbingyi.blog.51cto.com/5220457/1754797
以上是关于C#入门经典学习笔记 <chapter06 函数>的主要内容,如果未能解决你的问题,请参考以下文章