C# HashSet Add

Description

HashSet Add adds the specified element to a set.

Syntax

HashSet.Add has the following syntax.


public bool Add(
  T item
)

Parameters

HashSet.Add has the following parameters.

  • item - The element to add to the set.

Returns

HashSet.Add method returns true if the element is added to the HashSet object; false if the element is already present.

Example

The following example demonstrates how to create and populate two HashSet objects.


using System;//w  ww .  j  a  v a2  s  .c om
using System.Collections.Generic;

public class MainClass
{
    public static void Main(String[] argv)
    {
        HashSet<int> evenNumbers = new HashSet<int>();
        for (int i = 0; i < 5; i++)
        {
            evenNumbers.Add(i * 2);
        }
    }
}




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack