1 Private Void Simplebutton#click ( Object Sender, eventargs E)
2 {
3 List < Testclass > List = New List < Testclass > ();
4 Int [] Ary = New Int [] { 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 4 };
5
6 For ( Int I = 0 ; I < 100 ; I ++ )
7 {
8 List. Add ( New Testclass (I, I ));
9 }
10
11 VaR list2 = From t In List
12 Select New Testclass (T. x, t. y );
13
14 Foreach (VAR t In List2)
15 {
16 While ( True )
17 {
18 MessageBox. Show (T. gethashcode (). tostring () + " : " + List2.tolist ()[ 0 ]. Gethashcode (). tostring ());
19 MessageBox. Show (list2.tolist (). indexof (T). tostring ());
20 Return ;;
21 }
22 }
23
24 }
25
26 Public Class Testclass
27 {
28 Public Testclass ( Int X, Int Y)
29 {
30 This . X = X;
31 This . Y = Y;
32 }
33
34 Int X;
35 Public Int X
36 {
37 Get
38 {
39 Return X;
40 }
41 Set
42 {
43 X = Value;
44 }
45 }
46
47 Int Y;
48 Public Int Y
49 {
50 Get
51 {
52 Return Y;
53 }
54 Set
55 {
56 Y = Value;
57 }
58 }
59 }
60
The gethashcode values are different. The retrieval result is-1. It is reasonable to say no. I interpret the objects in list2 from the list2 set.
Key Code If it is changed to the following method, you can save the result to a collection first. 1 VaR list2 = From t In List
2 Select New Testclass (T. x, t. y );
3 VaR list3 = List2.tolist ();
4 Foreach (VAR t In List3)
5 {
6 While ( True )
7 {
8 MessageBox. Show (T. gethashcode (). tostring () + " : " + List3.tolist ()[ 0 ]. Gethashcode (). tostring ());
9 MessageBox. Show (list3.tolist (). indexof (T). tostring ());
10 Return ;;
11 }
12 }
13
I don't know the reason, but I found this problem. If the man knows it, I will post it. Thank you.