Microsoft address
Indexof () method of list <t>
If t is of the value type, match the first element of the list one by one based on the comparison value method. If t is of the reference type, compare whether the reference is the same.
Example:
class A { public int x; public A( int x) { this .x = x; } } List<A> listA = new List<A>(); listA.Add( new A(3) ); listA.Add( new A(4) ); listA.Add( new A(5) ); listA.Add( new A(54) ); Console.WriteLine( listA.IndexOf( new A(3) ) ); |
The custom class is a reference type. Therefore, indexof searches for elements by reference. Of course, it cannot be found. Print-1. If expression A is defined as a struct, it can be found, print 0
The remove method also applies. The removal method depends on the T type.
Except that the remove methods of hashset and list are slightly different:
Duplicate elements are not allowed in hashset <t>, and list <t> is allowed. If the remove method is called by hashset <t>, you can determine that there are no elements in this set that have just been removed, and list <t> calls the remove (T1) method to remove only the first matched element, there cannot be any other elements in this set that are equal to T1.
Indexof method and remove method of list <t>