C # bug discovered in the past two days

Source: Internet
Author: User

One is hashtable. If uint is used as the primary key when the add method is called, int is used to retrieve the content. I think there should be no difference between the two types in hash processing.

One is anonymous delegation. I think there is a problem when passing a local variable. For the value type, it is not a copy of the value, and the change of this value is actually reflected in the delegate call. the following is the test code.

Delegate void print ();

Class Program

{

Static void Main (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 ++)

{// When constructing a delegate, the value of I is 0, 1.

// B [2] is used for delegate calls.

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 );

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.