Indexof method and remove method of list <t>

Source: Internet
Author: User
Tags microsoft address

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>

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.