sunwen教程之----c#進階
(二)
[email protected]
好了,i come back,下面的代碼顯示了我們如何打印多個參數:
000: // commandline/cmdline1.cs
001: using system;
002:
003: public class commandline
004: {
005: public static void main(string[] args)
006: {
007: console.writeline("number of command line parameters = {0}", args.length);
008: for(int i = 0; i < args.length; i++)
009: {
010: console.writeline("arg[{0}] = [{1}]", i, args[i]);
011: }
012: }
013: }
如果你的輸入為:
cmdline1 a b c
那么它的輸出為:
number of command line parameters = 3
arg[0] = [a]
arg[1] = [b]
arg[2] = [c]
哈哈,看出了其中的秘密了吧!是的{0}是一個標記,它告訴系統,這里留下了給第0個參數用,在程序中,這第0個參數就是i.因此,打印出來的就是arg[0],面不是arg[{0}]了,哈哈!
例二向大家展示了foreach這個語句的用法,其實在asp中,這個經常用到:
000: // commandline/cmdline2.cs
001: using system;
002:
003: public class commandline2
004: {
005: public static void main(string[] args)
006: {
007: console.writeline("number of command line parameters = {0}", args.length);
008: foreach(string s in args)
009: {
010: console.writeline(s);
011: }
012: }
013: }
很顯然,args是一個數組,而且是字符型的.foreach的意思就是把其中的所有元素都循環完.運行得出:
>cmdline2 john paul mary
number of command line parameters = 3
john
paul
mary
好了,同志們,休息一下,我要睡覺了!88,下次再見!
下一頁