AddIfNotExists

        <Extension>
        Public Sub AddIfNotExists(Of T)(list As ICollection(Of T), value As T)
            If Not list.Contains(value) Then
                list.Add(value)
            End If
        End Sub
Example:
Dim exampleList As List(Of Long) From {1, 3} 

'Value will not be added because value exists
exampleList.AddIfNotExists(1)

'Value will be added because value not exists
exampleList.AddIfNotExists(2)

'Values in list: 1, 2 3

Description

An extension method for adding a value to a collection if it not exists yet.

Details

Double click on the code to select all.

 

;