4 out of 13 rated this helpful - Rate this topic

List(Of T).Contains Method

Updated: January 2010

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
'Usage
Dim instance As List 
Dim item As T
Dim returnValue As Boolean 

returnValue = instance.Contains(item)

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 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 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 
    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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

January 2010

Added example.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.