A set is a collection of values or objects so the list must respond to some Boolean conditions compared to another set. One operation would consist of finding whether two collections are the same because they respond to the same condition. Another operation would consist of finding out whether one list contains the same items as another but contains fewer items than the other collection, and so on. There are rules and conditions a set must follow:

Starting a Set

Consider the following list and notice that it contains an item twice:

The ICollection interface: This means that the collection is equipped with the ability to add new items and to know the number of items it contains

The ISerializable interface: this makes it possible to save (or serialize) the collection

Most of the time, in algebra, to study sets, we use numbers. In all of the examples in this lesson, we will use strings to make
them more interesting. Obviously, integers would be easier. If you want to use numbers, all you have to do is to replace all strings in our examples with
numbers.

Creating an Empty Set

In algebra, a set is empty if it contains no element. This is the case for a brand new set. It can also happen if the elements in the set have been deleted. In algebra, to create an empty set, use an uppercase letter and assign the phi character to it. Here is an example:

A = ∅

(In algebra, an empty set is also called a null set.) In algebra, to visually represent a set, we draw a circle.
If the set is empty, the circle is blank:

To programmatically create an empty set, declare a variable of type HashSet and use the new operator to initialize it.

Depending on the type of set, one set can be made of natural numbers, another made of floating-point numbers, yet another one made of strings or other types of values (dates, times, etc). When programmatically creating a set, pass the type of items in the angle brackets. Here is an example:

Unless a set is empty, it must have or contain something. An object included in a set is called an element or a member of the set. In algebra, to create a set, we specify an uppercase letter, followed by = { }. In the curly brackets, we list the elements separated by commas. Here is an example:

A = { Kevin, Jane, Daniel, Ruth, Paul }

To visually illustrate, you can represent each element in the circle. Here is an example:

To let you add an item to a set, the HashSet class is equipped with a method named Add. Its syntax is:

public bool Add(T item);

Pass the desired value as argument to this method. Remember that the class implements the IEnumerable<> interface and therefore the foreachloop. Here are examples of calling the Add() method to populate a set:

Notice that each element in this case is a constant, or has a constant value.

Remember that one of the rules of a set is that it cannot have duplicate items. If you add an item that exists in the list already, that item would not be added. In fact, to let you know whether an item was added, the HashSet<>.Add() method returns a Boolean value:

If the item is successfully added, the method returns true

If the addition is denied, the method returns false and (but) the compiler does not throw an exception

Accessing Each Element of a Set

The HashSet class implements the IEnumerable<T> and the IEnumerable interfaces. This makes it possible to use the foreach loop to access each item of a set. Here is an example:

In algebra, when it comes to the number of elements in a set, there are two categories. A set is referred to as finite if it contains a constant number of items. An example is the number of students in a classroom. A set is infinite if its number of items cannot be determined (or the items cannot be counted).
An example is the number of stars in the sky.

The HashSet class implements the ICollection<> generic interface, which gives it a property named Count. This property allows you to know the number of elements in a set. Here is an example:

Notice that the property produces the actual number of items in the collection, excluding those that were not added (because of duplication issues).

Checking Whether a Set Contains a Certain Element

In algebra, to represent that a certain
value "a" exists in a set A, we would write:

a ∈ A

This is read as "a is an element of set A" or "a is included in set A" or "Set A contains a". To support this operation, the HashSet class inherits the Contains() method from the ICollection<> interface. The method allows you to check whether the list already contains a certain item. Here are examples of calling this method:

Notice that both sets
have the same elements. When two sets have exact same members, we say that those sets are equal. This can be
expressed as:

A = B

This operation is commutative, which means the above operation
can also be written as:

B = A

We already know that all classes used in a C# application (or a .NET project) derive from the Object class and they inherit a method named Equals. This makes it possible to compare two values or two objects for equality. The HashSet class also naturally inherits this method. Although you can use that method, the HashSet class provides its own custom method named SetEquals. Its syntax is:

public bool SetEquals(IEnumerable<T> other);

This method takes as argument another HashSet list and compares both sets. If the sets are the same, the method returns true. Otherwise it produces a false value. Here are examples of calling this method:

It is important to know that the subset relationship is one way; in other words, the comparison is not commutative: the fact that a set A is a subset of a set B is not vice-versa.

A Proper Subset of Another Set

A set is a subset of itself. Also, when two sets have the exact same members, each set is a subset of the other. Consider the following illustration of sets:

If you have a set A that is strictly a subset of another set B, this means there is at least one element in set B that is not a member of set A. In this case, we say that set A is a proper subset of set B. This can be illustrated as follows:

To let you find out if a set is a proper subset of another set, the HashSet class is equipped with a method named IsProperSubsetOf
. Its syntax is:

public bool IsProperSubsetOf(IEnumerable<T> other);

This method takes a HashSet list as argument and performs
a comparison on their elements:

If both sets are the same, the method returns false

If the argument has at least one element that is not in the variable that called it, the method returns false

If all elements of the argument's set are members of the variable that called it, the method returns true

Here are two examples of calling this method:

<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
System.Collections.Generic.HashSet<string> censusMidAtlantic = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>();
censusMidAtlantic.Add("New York");
censusMidAtlantic.Add("New Jersey");
censusMidAtlantic.Add("Pennsylvania");
beaMideast.Add("Pennsylvania");
beaMideast.Add("New York");
beaMideast.Add("New Jersey");
}
@if(censusMidAtlantic.IsProperSubsetOf(beaMideast) == true)
{
<p>The Mid-Atlantic region of the Census Bureau is a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p>
}
else
{
<p>The Mid-Atlantic region of the Census Bureau is NOT a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p>
}
@{
beaMideast.Add("Delaware");
beaMideast.Add("District of Columbia");
beaMideast.Add("Maryland");
}
@if (censusMidAtlantic.IsProperSubsetOf(beaMideast) == true)
{
<p>The Mid-Atlantic region of the Census Bureau is a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p>
}
else
{
<p>The Mid-Atlantic region of the Census Bureau is NOT a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p>
}
</body>
</html>
@if(censusMidAtlantic.IsProperSubsetOf(beaMideast) == true)
{
<p>The Mid-Atlantic region of the Census Bureau is a proper sub-set of the Mideast region of the Bureau of Economic Analysis.</p>
}
else
{
<p>The Mideast region of the Bureau of Economic Analysis has more states than the Mid-Atlantic region of the Census Bureau.</p>
}
</p>
</body>
</html>

This would produce:

A Super-Set of an Existing Set

Remember that a sub-set is a set whose all elements are also found in another set. A super-set is the reverse of a sub-set. That is, in a superset, all the elements of a set B are found in a set A but set A may have elements that are not found in B. In algebra, this can be written as follows:

B ⊃ A

To help you make this comparison, the HashSet class is equipped with a method named IsSupersetOf. Its syntax is:

public bool IsSupersetOf(IEnumerable<T> other);

This method takes a HashSet object as argument and compares its elements to those of the variable that called it. If all the elements of the argument are found in the variable that called it, the method returns true. Here are examples of calling this method:

When it comes to a super-set, if both sets are the same, each is considered a super-set of the other and the IsSupersetOf() method returns true. By contrast, if a set Bis a super-set of A but both sets
are not the same, that is, set B has more elements than set A, set B is said to be a proper super-set of A. To let you make the comparison to determine this, the HashSet class provides a method named IsProperSupersetOf. Its syntax is:

public bool IsProperSupersetOf(IEnumerable<T> other);

Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Country's Regions</title>
</head>
<body>
<h2>Country's Regions</h2>
@{
System.Collections.Generic.HashSet<string> beaMideast = new System.Collections.Generic.HashSet<string>();
System.Collections.Generic.HashSet<string> censusMidAtlantic = new System.Collections.Generic.HashSet<string>();
censusMidAtlantic.Add("New York");
censusMidAtlantic.Add("New Jersey");
censusMidAtlantic.Add("Pennsylvania");
beaMideast.Add("Pennsylvania");
beaMideast.Add("New York");
beaMideast.Add("New Jersey");
}
@if (beaMideast.IsSupersetOf(censusMidAtlantic) == true)
{
<p>The Mid-Atlantic region of the Bureau of Economic Analysis is a super-set of the Mideast region of the Census Bureau.</p>
}
else
{
<p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a super-set of the Mideast region of the Census Bureau.</p>
}
@if (beaMideast.IsProperSupersetOf(censusMidAtlantic) == true)
{
<p>The Mid-Atlantic region of the Bureau of Economic Analysis is a PROPER super-set of the Mideast region of the Census Bureau.</p>
}
else
{
<p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a PROPER super-set of the Mideast region of the Census Bureau.</p>
}
@{
beaMideast.Add("Delaware");
beaMideast.Add("District of Columbia");
beaMideast.Add("Maryland");
}
@if (beaMideast.IsProperSupersetOf(censusMidAtlantic) == true)
{
<p>The Mid-Atlantic region of the Bureau of Economic Analysis is a PROPER super-set of the Mideast region of the Census Bureau.</p>
}
else
{
<p>The Mid-Atlantic region of the Bureau of Economic Analysis is NOT a PROPER super-set of the Mideast region of the Census Bureau.</p>
}
</body>
</html>

One of the operations you can perform
on them is to create a set that contains a list of all their elements. That is, you would get a list that contains elements that are present in each set. If there is an element present in both sets, it would be included only once.

A union of two sets is the list of all elements that belong to both sets. Remember that there is no reason to include an element twice if it already belongs to one of the sets. In algebra, this operation is written as follows:

A ∪ B = { x|x ∈ A or x ∈ B }

This can be visually illustrated as follows:

To help you unite two sets, the HashSet class is equipped
with a method named Union. Its syntax is:

public void UnionWith(IEnumerable<T> other);

This method takes as argument the set whose elements would be combined to those of the set that called the method. Here is an example of calling this method:

It is important to know that this method performs a comparison on the elements of both sets so that the result would not allow any duplicate item.

A Universal Set

If you have two or more sets, a universal set is a list that contains all elements of those sets. For example, if you have three sets A, B, and C, you can write their universal set as:

A ∪ B ∪ C

To programmatically create a universal set, simply call the HashSet<>.UnionWith() method as necessary.

An Intersection of Sets

The intersection between two sets is the list of only members that belong to both sets. That is, the elements that belong to one set but do not belong to the other set are excluded. In algebra, we write it as follows:

A ∩ B = { x|x ∈ A and x ∈ B }

This can be illustrated as follows:

To help you reduce a set to its intersecting elements with regards to another set, the HashSet class is equipped with a method named IntersectWith(). Its syntax is:

public void IntersectWith(IEnumerable<T> other);

This method takes a HashSet collection as argument. It finds the elements from the argument that are (the elements) also present in the variable that called it and it removes
the elements that are found in the variable that called it but are not found in the argument. Here is an example:

As seen above, the intersecting set is a list of elements shared by two sets. The comparison consists of finding out whether two lists intersect; that is, whether they have at least one common member. To support this comparison, the the HashSet class is equipped with a method named Overlaps. Its syntax is:

public bool Overlaps(IEnumerable<T> other);

This method too takes a HashSet object as argument and compares it to the object that called it. If it finds at least one value that exists in both lists, it returns true. If there is no single value that both lists share, the
method returns false. Here is an example:

Consider two sets A and B. The symmetric difference between those sets is the list of elements that belong to each set but do not belong to their intersection. This can be illustrated as follows:

As you can guess, this is the reverse of the intersection. That is, it the union of the sets excluding their intersection. To let you perform this operation, the
HashSet is equipped with a method named SymmetricExceptWith. Its syntax is:

In some cases, you may want to delete an element based on a specific condition. To support this operation, the HashSet class provides a method named RemoveWhere. Its syntax is:

public int RemoveWhere(Predicate<T> match);

This method takes as argument a delegate that specifies what condition would be applied to any element that must be removed. Here is an example that asks the compiler to remove strings that contain a space: