• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • Beginner with C# 7

    發表于:2007-06-30來源:作者:點擊數: 標簽:
    1。7 語句(statements) c#借用了c/c++大多數的語句方法,不過仍然有些值得注意的地方。還有些地方是有所改動的。 在這里,我只提一些c#特有的東東。 1。7。10 “foreach”語句 “foreach”語句列舉一個集合內的所有元素,并對這些元素執行一系列的操作。還
         1。7 語句(statements)
      
      c#借用了c/c++大多數的語句方法,不過仍然有些值得注意的地方。還有些地方是有所改動的。
      在這里,我只提一些c#特有的東東。
      
      1。7。10 “foreach”語句
      “foreach”語句列舉一個集合內的所有元素,并對這些元素執行一系列的操作。還是看看例子吧:*/
      
      using system;
      using system.collections;
      class test
      {
       static void writelist(arraylist list) {
       foreach (object o in list)
       {
       int i = (int) o;//如果是for語句,這里一定會報錯!
       console.writeline(0);
       console.writeline(++i);
       }
       }
       static void main() {
       arraylist list = new arraylist();
       for (int i = 0; i < 10; i++)
       list.add(i);
       writelist(list);
       }
      }
      /*這個例子用“foreach”掃描了整個“list”,并把“list”中所有的元素打印出來。有時候還是
      挺方便的。
      
      1。7。15 安全檢查開關(the checked and unchecked statements)
      “checked”和“unchecked”語句用來控制數學運算和完整類型轉換的檢查工作?!癱hecked”檢查它
      作用的域中可能出現的違例,并拋出一個異常;而“unchecked”則阻止所有的檢查。舉個例子:*/
      
      using system;
      class test
      {
       static int x = 1000000;
       static int y = 1000000;
       static int f() {
       checked {return (x * y);} // 拋出 overflowexception
       }
       static int g() {
       unchecked {return (x * y);} // 返回 -727379968
       }
       static int h() {
       return x * y; // 缺省狀態。
       }
       static void main() {
       f(); //可以注銷掉此行試試。
       console.writeline(g());
       console.writeline(h());
       }
      }
      
      /*
      在編譯過程中不會有任何錯誤出現。因為“checked”和“unchecked”只在運行時才起作用。值得一說的是
      h()。它的缺省狀態和編譯器當前的缺省溢出檢查的狀態有關。但返回的結果肯定和f()或g()中的任一個相同。
      再看一個例子:*/
      
      using system;
      class test
      {
       const int x = 1000000;
       const int y = 1000000;
       static int f() {
       checked {return (x * y);} // 編譯器警告(compile warning):溢出(overflow)
       }
       static int g() {
       unchecked {return (x * y);} // 返回 -727379968
       }
       static int h() {
       return x * y; // 編譯器警告(compile warning):溢出(overflow)
       }
       static void main() {
       console.writeline(f()); //可以注銷掉此行試試。
       console.writeline(g());
       console.writeline(h()); //可以注銷掉此行試試。
       }
      }
      
      /* 當f()和h()求值的時候,就會引起一個編譯警告。而在g()中,因為有了“unchecked”,屏蔽了這個警
      告。要注意的是“checked”和“unchecked”都不能對函數的返回值進行操作!比如:*/
      class test
      {
       static int multiply(int x, int y) {
       return x * y;
       }
       static int f() {
       checked{ return multiply(1000000, 1000000); } // 與 return multiply(1000000, 1000000);
       } // 有相同的效果。
      }
      /* 其實大家稍微想一下知道為什么m$沒有這么做!對這個內容的討論超出本文的范圍和俺的能力之外哦。
      
      在c#中,所有的十六進制數都是uint。如果用強制類型轉換會引起編譯器報錯。用“unchecked”則可以
      跳過這個機制,把uint的十六進制數轉化為int。如:*/
      
      class test
      {
       public const int allbits = unchecked((int)0xffffffff);
       public const int highbit = unchecked((int)0x80000000);
      }
      
      /* 上例所有的常數都是uint,而且超過了int的范圍,沒有“unchecked”,這種轉換會引發一個編譯器錯
      誤。注意:上面用的是“unchecked”操作符。不是語句。不過它們之間除了一個用“()”,另一個用
      “{}”以外,幾乎一樣。btw,“checked”同樣。
      
      1。7。16 “lock”語句(the lock statement)
      “lock”獲得一個相互排斥的對象鎖定。(俺查過一些資料,但都沒有清晰說明,暫不介紹)
      
      
      

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>