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 FunctionExample:
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
- Author: Blake Pell
- Submitted on: 13-4-2010 19:02:15
- Language: VB
- Type: System.Collections.Generic.IList<T>
- Views: 2318
Double click on the code to select all.