這兩天發現的C#bug

來源:互聯網
上載者:User

一個是Hashtable,如果在調用Add方法加入時用uint作為主鍵,取的使用用int,就不能取出放入的內容.我覺得這兩種類型在進行hash處理時應該沒有區別才對

一個是匿名委託,我覺得傳遞局部變數時有問題,對於實值型別,傳遞的不是該值的一個拷貝,該值的變化居然會反映到委託調用時.下面是測試代碼

   delegate void Print();

   class Program

   {

      static voidMain(string[] args)

      {

        probDelegate();

      }

 

      static void probHashtable()

      {

        Hashtable t = new Hashtable();

        t.Add((uint)1, "hello");

        object o = t[1];

        object o2 = t[1U];

        //o will get nothing

        //o2 will get the problem value

        Console.WriteLine("o={0} o2={1}",o,o2);

      }

 

      static void probDelegate()

      {

        int[] b=new int[]{1,2,3};

        Print[] ps= new Print[2];

        for (int i = 0; i < 2; i++)

        {//構造委託時i的值是0,1

        //但在委託調用時卻使用的是b[2]

           ps[i] = delegate{ doPrint(b[i]);};

        }

 

        for (int i = 0; i < ps.Length; i++)

        {

           ps[i]();

        }

        //the result is 3 3. NOT 1 2

      }

 

      static void doPrint(int d)

      {

        Console.WriteLine(d);

      }

   }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.