经典实例
Code string mystring = " this is a test " ; char [] separator = { ' ' }; string [] myWords; myWords = mystring.Split(separator); foreach ( string word in myWords) { Console.WriteLine( " {0} " ,word); } Console.ReadKey(); 实例2
Code static void Main( string [] args) { int [] myArray = { 1 , 8 , 3 , 6 , 2 , 5 , 9 , 3 , 0 , 2 }; int maxVal = MaxValue(myArray); Console.WriteLine( " 最大值为:{0} " , maxVal); Console.ReadKey(); } static int MaxValue( int [] intArray) { int maxVal = intArray[ 0 ]; for ( int i = 1 ; i < intArray.Length; i ++ ) { if (intArray[i] > maxVal) maxVal = intArray[i]; } return maxVal; } 实例3:参数数组
Code static void Main( string [] args) { int sum = SumVals( 1 , 3 , 4 , 5 , 2 , 5 ); Console.WriteLine( " 和为:{0} " , sum); Console.ReadKey(); } static int SumVals( params int [] vals) { int sum = 0 ; foreach ( int val in vals) { sum += val; } return sum; }
实例4:委托
Code 1 static void Main( string [] args) 2 { 3 ProcessDelegate process; 4 Console.WriteLine(" 请输入两个数: " ); 5 string input = Console.ReadLine(); 6 int commapos = input.IndexOf( ' , ' ); 7 double param1 = Convert.ToDouble(input.Substring( 0 , commapos)); 8 double param2 = Convert.ToDouble(input.Substring(commapos + 1 , input.Length - commapos - 1 )); 9 Console.WriteLine(" 请选择是相乘(M)还是相除(D): " ); 10 input = Console.ReadLine(); 11 12 if (input == " M " ) 13 process = new ProcessDelegate(Multiply); 14 else 15 process = new ProcessDelegate(Divide); 16 Console.WriteLine(" Result:{0} " , process(param1, param2)); 17 Console.ReadKey();18 }19 20 delegate double ProcessDelegate( double param1, double param2); 21 22 static double Multiply( double param1, double param2) 23 { 24 return param1 * param2; 25 }26 27 static double Divide( double param1, double param2) 28 { 29 return param1 / param2; 30 }
posted on
2009-04-21 22:50
土农民
阅读(
254 )
评论()
收藏
举报