Sub set operations

In this chapter you will learn:

  1. Sub set operations

Sub set operations

using System;/*from   j  a v  a2 s  .c  o  m*/
using System.Collections.Generic;

class Program
{

    static void Main()
    {
        HashSet<int> lowNumbers = new HashSet<int>();
        HashSet<int> allNumbers = new HashSet<int>();
    
        for (int i = 1; i < 5; i++)
        {
            lowNumbers.Add(i);
        }
    
        for (int i = 0; i < 10; i++)
        {
            allNumbers.Add(i);
        }
    
        Console.Write("lowNumbers contains {0} elements: ", lowNumbers.Count);
        Console.Write("allNumbers contains {0} elements: ", allNumbers.Count);
        Console.WriteLine("lowNumbers overlaps allNumbers: {0}",lowNumbers.Overlaps(allNumbers));
        Console.WriteLine("allNumbers and lowNumbers are equal sets: {0}",allNumbers.SetEquals(lowNumbers));
        Console.WriteLine("lowNumbers is a subset of allNumbers: {0}",lowNumbers.IsSubsetOf(allNumbers));
        Console.WriteLine("allNumbers is a superset of lowNumbers: {0}",allNumbers.IsSupersetOf(lowNumbers));
        Console.WriteLine("lowNumbers is a proper subset of allNumbers: {0}",lowNumbers.IsProperSubsetOf(allNumbers));
        Console.WriteLine("allNumbers is a proper superset of lowNumbers: {0}",allNumbers.IsProperSupersetOf(lowNumbers));
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use for loop to get all keys from a Hashtable
Home » C# Tutorial » Collections
HashSet
Set operation: Except
Set operator intersect
Remove with condition
HashSet contains
HashSet creation
HashSet removing element
Sub set operations
Hashtable
Hashtable ContainsValue
Add elements to a Hashtable
Hashtable containsKey
Remove from Hashtable
Hashtable to array
Queue
Generic Queue
Stack