Export (0) Print
Expand All

List(Of T).Contains Method

Determines whether an element is in the List(Of T).

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function Contains ( _
	item As T _
) As Boolean

Parameters

item
Type: T

The object to locate in the List(Of T). The value can be Nothing for reference types.

Return Value

Type: System.Boolean
true if item is found in the List(Of T); otherwise, false.

Implements

ICollection(Of T).Contains(T)

This method determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable(Of T).Equals method for T (the type of values in the list).

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

The following example demonstrates the Contains and Existsmethods on a List(Of T) that contains a simple business object that implements Equals.

Imports System.Collections.Generic
' Simple business object. A PartId is used to identify a part  
' but the part name can change.  
Public Class Part
    Implements IEquatable(Of Part)
    Public Property PartName() As String 
        Get 
            Return m_PartName
        End Get 
        Set(value As String)
            m_PartName = Value
        End Set 
    End Property 
    Private m_PartName As String 
    Public Property PartId() As Integer 
        Get 
            Return m_PartId
        End Get 
        Set(value As Integer)
            m_PartId = Value
        End Set 
    End Property 
    Private m_PartId As Integer 

    Public Overrides Function ToString() As String 
        Return Convert.ToString("ID: " & PartId & "   Name: ") & PartName
    End Function 
    Public Overrides Function Equals(obj As Object) As Boolean 
        If obj Is Nothing Then 
            Return False 
        End If 
        Dim objAsPart As Part = TryCast(obj, Part)
        If objAsPart Is Nothing Then 
            Return False 
        Else 
            Return Equals(objAsPart)
        End If 
    End Function 
    Public Overrides Function GetHashCode() As Integer 
        Return PartId
    End Function 
    Public Overloads Function Equals(other As Part) As Boolean _
        Implements IEquatable(Of Part).Equals
        If other Is Nothing Then 
            Return False 
        End If 
        Return (Me.PartId.Equals(other.PartId))
    End Function 
    ' Should also override == and != operators. 
End Class 
Public Class Example
    Public Shared Sub Main()
        ' Create a list of parts. 
        Dim parts As New List(Of Part)()

        ' Add parts to the list.
        parts.Add(New Part() With { _
             .PartName = "crank arm", _
             .PartId = 1234 _
        })
        parts.Add(New Part() With { _
             .PartName = "chain ring", _
             .PartId = 1334 _
        })
        parts.Add(New Part() With { _
             .PartName = "regular seat", _
             .PartId = 1434 _
        })
        parts.Add(New Part() With { _
             .PartName = "banana seat", _
             .PartId = 1444 _
        })
        parts.Add(New Part() With { _
             .PartName = "cassette", _
             .PartId = 1534 _
        })
        parts.Add(New Part() With { _
             .PartName = "shift lever", _
             .PartId = 1634 _
        })



        ' Write out the parts in the list. This will call the overridden ToString method 
        ' in the Part class.
        Console.WriteLine()
        For Each aPart As Part In parts
            Console.WriteLine(aPart)
        Next 


        ' Check the list for part #1734. This calls the IEquitable.Equals method 
        ' of the Part class, which checks the PartId for equality.
        Console.WriteLine(vbLf & "Contains: Part with Id=1734: {0}",
                          parts.Contains(New Part() With { _
             .PartId = 1734, _
             .PartName = "" _
        }))

        ' Find items where name contains "seat".
        Console.WriteLine(vbLf & "Find: Part where name contains ""seat"": {0}",
                          parts.Find(Function(x) x.PartName.Contains("seat")))

        ' Check if an item with Id 1444 exists.
        Console.WriteLine(vbLf & "Exists: Part with Id=1444: {0}",
                          parts.Exists(Function(x) x.PartId = 1444))

        'This code example produces the following output: 
        '         
        '        ID: 1234   Name: crank arm 
        '        ID: 1334   Name: chain ring 
        '        ID: 1434   Name: regular seat 
        '        ID: 1444   Name: banana seat 
        '        ID: 1534   Name: cassette 
        '        ID: 1634   Name: shift lever 
        
        '        Contains: Part with Id=1734: False 
        
        '        Find: Part where name contains "seat": ID: 1434   Name: regular seat 
        
        '        Exists: Part with Id=1444: True  
        '          

    End Sub 
End Class

The following example contains a list of complex objects of type Box. The Box class implements the IEquatable(Of T).Equals method so that two boxes are considered equal if their dimensions are the same. In this example, the Contains method returns true, because a box that has the specified dimensions is already in the collection.

Imports System
Imports System.Collections.Generic

Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim boxes As New List(Of Box)()

        boxes.Add(New Box(8, 8, 4))
        boxes.Add(New Box(8, 4, 8))
        boxes.Add(New Box(8, 6, 4))

        If boxes.Contains(New Box(8, 6, 4)) Then
            Console.WriteLine("An equal box is already in the collection.")
        Else
            Console.WriteLine("Box can be added.")
        End If 

	'Outputs "An equal box is already in the collection." 
    End Sub 
End Class 

Public Class Box
    Implements IEquatable(Of Box)

    Public Sub New(ByVal h As Integer, ByVal l As Integer, ByVal w As Integer)
        Me.Height = h
        Me.Length = l
        Me.Width = w
    End Sub 
    Private _Height As Integer 
    Public Property Height() As Integer 
        Get 
            Return _Height
        End Get 
        Set(ByVal value As Integer)
            _Height = value
        End Set 
    End Property 
    Private _Length As Integer 
    Public Property Length() As Integer 
        Get 
            Return _Length
        End Get 
        Set(ByVal value As Integer)
            _Length = value
        End Set 
    End Property 
    Private _Width As Integer 
    Public Property Width() As Integer 
        Get 
            Return _Width
        End Get 
        Set(ByVal value As Integer)
            _Width = value
        End Set 
    End Property 

    Public Overloads Function Equals(ByVal other As Box) _
    		As Boolean Implements IEquatable(Of Box).Equals
        If Me.Height = other.Height And Me.Length = other.Length _
        		And Me.Width = other.Width Then 
            Return True 
        Else 
            Return False 
        End If 
    End Function 
End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft