1
MSDN Library MSDN Library 量 例 參 列 [ 說 ] [] [ 索 ] [] 來 MSDN Library 了 類 類 利 F1 http://msdn.microsoft.com/library/ http://msdn.microsoft.com/library/cht/
Object object 參 類 都 object 參 object Boxing 參 boxing ex object o = (int) 1; UnBoxing UnBoxing ex int i = (int) o;
ArrayList 類 (1) 列 列 不 便 ArrayList 料量 列 行 度 列不 不
ArrayList 類 (2) System.Collections; Count 數 數 Add Insert Remove Clear Sort Reverse
例 sample6-a1 (1) ArrayList 列 ArrayList class car public car(int x) id = x; public int id;
例 sample6-a1 (2) static void Main(string[] args) ArrayList myal = new ArrayList(); car mycar = new car(10); myal.add(12); myal.add("ives"); myal.add(mycar); Console.WriteLine("myAL[0] 1",0,myAL[0]); Console.WriteLine("myAL[0] 1",1,myAL[1]); Console.WriteLine("myAL[0] 1",2,((car)myAL[2]).id); ((car)myal[2]).id = 100; Console.WriteLine("myCar " + mycar.id);
練 sample6-b1 數 料 ( 不 數 ) 數 料 列 念 利 ArrayList 來 料 利 Sort 料 行
列類 Rank 列 度 Length 列 數 GetLength 度 度 Clone 列 列 ( 不 參 參 ) Sort 列 IndexOf 參數 索
例 sample6-a2 (1) 列類 參 class car public int id;
例 sample6-a2 (2) static void Main(string[] args) int[,] x = new int[2,3]; Console.WriteLine("Length " + x.length); Console.WriteLine("Rank " + x.rank); Console.WriteLine("GetLength(0) " + x.getlength(0)); Console.WriteLine("GetLength(1) " + x.getlength(1));
例 sample6-a2 (3) car[] mycar = new car[5]; for(int i=0; i<mycar.length; i++) mycar[i] = new car(); mycar[i].id = i; Console.WriteLine(); // 參 x[0,0] = 100; mycar[0].id = 100;
例 sample6-a2 (4) int[,] y = (int[,]) x.clone(); car[] mymoto = (car[]) mycar.clone(); y[0,0] = 0; mymoto[0].id = 0; Console.WriteLine("x[0,0] " + x[0,0]); Console.WriteLine("y[0,0] " + y[0,0]); Console.WriteLine("myCar[0].id " + mycar[0].id); Console.WriteLine("myMoto[0].id " + mymoto[0].id);
練 sample6-b2 立 int 列 10 1 ~ 10 數 (1~10) 列 數 列 索 念 利 IndexOf
串類 Length 串 數 ( 度 ) ToUpper / ToLower 串 Insert 串 PadLeft / PadRight 度 IndexOf 串 Split 串 切 串 列 Replace 串 SubString 串
例 sample6-a3 串類 string str = "Alice is a good student!!"; Console.WriteLine(str); Console.WriteLine("ToUpper " + str.toupper()); Console.WriteLine("ToLower " + str.tolower()); Console.WriteLine("ToLower " + str.insert(6,"wang ")) string[] strarray = str.split(' '); for(int i=0; i<strarray.length; i++) Console.WriteLine(strArray[i]);
練 sample6-b3 數 ( ) 列 數 念 利 Split 串 數
C c Console.WriteLine("0:c",4.5); //NT$4.50 數 Dn dn Console.WriteLine("0:d5",4); //00004 E e Console.WriteLine("0:e",4); //4.000000e+001 Fn fn Console.WriteLine("0:f3",4); //4.000 數 Nn nn Console.WriteLine("0:n2",4000); //4,000.00 六 X x Console.WriteLine("0:x",10); //a
數 串 0 數 數 數 料 0 數 # 數 數 數 料 數. 數 數 數 數 數 理, 數 1000 1000000 % 數 100
例 sample6-a4 (1) 數 串 double myvar1 = 0801234567; Console.WriteLine(" 1." + myvar1.tostring("(0##)###- ####")); int myvar2 = -12345; Console.WriteLine(" 2." + myvar2.tostring("######")); Console.WriteLine(" 3." + myvar2.tostring("000000"));
例 sample6-a4 (2) double myvar3 = -2.455; Console.WriteLine(" 4." + myvar3.tostring("#.##")); Console.WriteLine(" 5." + myvar3.tostring("00.00")); Console.WriteLine(" 6." + myvar3.tostring("00.00000")); double myvar4 = 1234567890; Console.WriteLine(" 7." + myvar4.tostring("#,#")); Console.WriteLine(" 8." + myvar4.tostring("#,")); Console.WriteLine(" 9." + myvar4.tostring("#,,")); Console.WriteLine("10." + myvar4.tostring("#,,,")); double myvar5 = 0.086; Console.WriteLine("11." + myvar5.tostring("#.#%"));
類 DateTime ( 年 ) Math 數 數 數 見數 數 Random 亂數
例 sample6-a5 DateTime Math 類 Console.WriteLine(" " + DateTime.Now); Console.WriteLine("2 10 " + Math.Pow(2,10)); Console.WriteLine("-2 " + Math.Abs(-2)); Console.WriteLine("Sin(pi/2) " + Math.Sin(Math.PI/2));
練 sample6-b4 年 列 年 年 念 利 DateTime 年 來
不 邏 邏 念 利 來 行 料 不 0 行 利 try catch 來
(1) 行 來 數 數 滑 滑
(2) 行 行行 行 數數 行 [ 行 ] 行 行 行 不 [ 不 ] 行 行 不 行
(3) 行 [] [] [ 數 ] [ 數 ] [ ] 來 數 數 行行 數 數 行 數 數
例 sample6-a6 數 x = 10; int c = 0; for(int i=0; i<10; i++) c+=i; c += x; ArrayList myal = new ArrayList(); myal.add(c); x = (int)myal[0];
例 理 (1) 行 讀 try catch finally 來 行 try catch 便 行 catch 理 不 catch 都 行 finally
例 理 (2) 例 類 都 Exception 例 類 例 類 類 Exception DivideByZeroException IndexOutOfRangeException InvalidCastException OverflowException 說 行 數 0 索 列 說 Message 例
例 理 (3) try ; catch ( 例 類 數 ) ; catch ( 例 類 數 ) ; finally ;
例 理 (4) try int x = 0; x = 3/x; catch Console.WriteLine("!!"); finally Console.WriteLine("");
例 sample6-a7 (1) 數 x 3 x try Console.Write(" 數 "); int x = int.parse(console.readline()); Console.WriteLine("" + (3/x));
例 sample6-a7 (2) catch Console.WriteLine("!!"); finally Console.WriteLine("");
練 sample6-b5 數 x x 串 列 念 例 理來 串
例 理 throw 來 throw 例 類 throw new 例 類 (); throw new Exception("error!!");
例 sample6-a8 (1) 例 static int getmonth() Console.Write(""); int month = int.parse(console.readline()); if(month > 12 month <= 0) throw new Exception(" 不!!"); else return month;
例 sample6-a8 (2) static void Main(string[] args) try Console.WriteLine("" + getmonth()); catch(exception e) Console.WriteLine(e.Message); finally Console.ReadLine();