AddIfDoesntExist

    ''' <summary>
    ''' Adds an object to the list only if it does not already exist in the list.
    ''' </summary>
    ''' <param name="ls"></param>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function AddIfDoesntExist(Of T)(ByVal ls As List(Of T), ByVal value As T) As List(Of T)
        If ls.Contains(value) = False Then
            ls.Add(value)
        End If

        Return ls
    End Function
Example:
        Dim ls As New List(Of String)

        ls.AddIfDoesntExist("Blake")
        ls.AddIfDoesntExist("Blake")
        ls.AddIfDoesntExist("Bob")
        ls.AddIfDoesntExist("William")
        ls.AddIfDoesntExist("Jacob")

Description

Adds an item to a generic list if it doesn't exit.

Details

Double click on the code to select all.

 

;