HashSet<T>.Add(T) Método

Definição

Adiciona o elemento especificado a um conjunto.

public:
 virtual bool Add(T item);
public:
 bool Add(T item);
public bool Add(T item);
abstract member Add : 'T -> bool
override this.Add : 'T -> bool
member this.Add : 'T -> bool
Public Function Add (item As T) As Boolean

Parâmetros

item
T

O elemento a adicionar ao conjunto.

Devoluções

true se o elemento for adicionado ao HashSet<T> objeto; false se o elemento já estiver presente.

Implementações

Exemplos

O exemplo seguinte demonstra como criar e preencher dois HashSet<T> objetos. Este exemplo faz parte de um exemplo maior fornecido para o UnionWith método.

HashSet<int> evenNumbers = new HashSet<int>();
HashSet<int> oddNumbers = new HashSet<int>();

for (int i = 0; i < 5; i++)
{
    // Populate numbers with just even numbers.
    evenNumbers.Add(i * 2);

    // Populate oddNumbers with just odd numbers.
    oddNumbers.Add((i * 2) + 1);
}
let evenNumbers = HashSet<int>()
let oddNumbers = HashSet<int>()

for i = 0 to 4 do
    // Populate numbers with just even numbers.
    evenNumbers.Add(i * 2) |> ignore

    // Populate oddNumbers with just odd numbers.
    oddNumbers.Add(i * 2 + 1) |> ignore

Observações

Se Count já for igual à capacidade do HashSet<T> objeto, a capacidade é automaticamente ajustada para acomodar o novo item.

Se Count for inferior à capacidade do array interno, este método é uma operação O(1). Se o HashSet<T> objeto tiver de ser redimensionado, este método torna-se uma operação O(n), onde n é Count.

Aplica-se a