Difference between Find, Exists and Contains of ListT class

5

I'm working with the class List<T> , so I came across some search methods, if I may call it that:

  • Find ;
  • Exists ;
  • Contains ;
  • I was wondering what the difference was between them, so I searched the Microsoft website: List Class , I came to the following conclusion:

  • Find : Searches for an element that matches the definitions, and returns the first value in the list.
  • Exists : Determines whether an element exists that matches the definitions.
  • Contains : Determines if an element exists that matches ALL definitions.
  • Could you give me a better explanation of the 3 methods? I think it's somebody who confuses the less experienced.

        
    asked by anonymous 18.05.2017 / 20:01

    2 answers

    4
      
  • Find : Searches for an element that matches the definitions, and returns the first value in the list.
  •   

    Correct. In other words, it corresponds to the predicate passed as a parameter.

      
  • Exists : Determines whether an element exists that matches the definitions.
  •   

    Yes, but idem item 1.

      
  • Contains : Determines if an element exists that matches ALL definitions.
  •   

    It would be better to say "whether the compared element is contained in the list or not".

    For the comparison, EqualityComparer<T>.Default is used. , which compares the references of the objects involved in the iterations. For simple variables it works fine. For complex objects, it is advisable to write a specific comparer in order to compare, for example, property ownership.

        
    18.05.2017 / 20:06
    0

    Explaining the same with other words.

      

    Find: Finds an element that matches the definitions, and returns the first value in the list.

    NO . It returns all elements - not just the first, it returns a list - that satisfy the query of the predicate.

      

    Exists: Determines whether an element exists that matches the definitions.

    Returns true when it finds the first element that satisfies the query of the predicate. This is important as it does not scroll through all items in the list, but in the first instance, it already returns true .

      

    Contains: Determines if there is an element that matches ALL definitions.

    Returns true , not "if there is an element", but if the element exists. The Contains() expects to receive an object of the same type from the list, and returns whether that object - the very instance of the object - is contained in the list or not.     

    29.06.2017 / 15:08