AddIfDoesntExist
Adds an item to a generic list if it doesn't exit.
Source
''' <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")
Author: Blake Pell
Submitted on: 13 apr. 2010
Language: VB
Type: System.Collections.Generic.IList<T>
Views: 4418