|
A set is a list of items with various goals:
- To know whether the set is empty or it contains at least one item
- To find out whether a set (already) contains a certain value
- To compare one set to another. Some of the sought results are to
find out if two sets are the same, if one set is less than the other,
etc
|
To support sets, the .NET Framework provides a class
named HashSet. This class is created in the
System.Collections.Generic namespace.
In algebra, a set is usually represented with an
uppercase letter, such as A. In programming, we will represent the list
with a name.
Consider the following list and notice that it
contains an item twice:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
// Use references to
// System
// System.Drawing
// System.Windows.Forms
public class SetHasher : Form
{
ListBox lbxNames;
public SetHasher()
{
InitializeComponent();
}
private void InitializeComponent()
{
lbxNames = new ListBox();
lbxNames.Location = new System.Drawing.Point(12, 12);
lbxNames.Size = new System.Drawing.Size(120, 95);
StartPosition = FormStartPosition.CenterScreen;
Text = "Set Hasher";
MinimizeBox = false;
MaximizeBox = false;
ClientSize = new System.Drawing.Size(145, 120);
Load += new EventHandler(FormLoaded);
Controls.Add(lbxNames);
}
private void FormLoaded(object sender, EventArgs ea)
{
List<string> names = new List<string>();
names.Add("Kevin");
names.Add("Jane");
names.Add("Daniel");
names.Add("Ruth");
names.Add("Jane");
names.Add("Paul");
lbxNames.DataSource = names;
}
}
public class Exercise
{
[STAThread]
public static int Main()
{
Application.Run(new SetHasher());
return 0;
}
}
In a .NET application, a set is a list that does not
allow duplicate items.
|
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.
|
Topic
Applied: Introducing Sets
|
|
- Start Microsoft Visual Studio or Microsoft Visual C# Express
- Create a Windows Application named Algebra2
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Algebra.cs and press Enter
- Click the body of the form to select it
- In the Properties window, change the following characteristics
FormBorderStyle: FixedDialog
Text: Operations on Sets
StartPosition: CenterScreen
MaximizeBox: False
MinimizeBox:
False
- Design the form as follows:
|
Control |
Text |
Name |
Label |
|
Number: |
|
TextBox |
|
|
txtNumberA |
Button |
|
Add |
btnAddA |
ListBox |
|
|
lbxA |
|
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:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
}
The HashSet class is defined as
follows:
[SerializableAttribute]
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public class HashSet<T> : ICollection<T>, IEnumerable<T>,
IEnumerable, ISerializable, IDeserializationCallback
Topic
Applied: Creating an Empty Set
|
|
- Double-click an unoccupied of the form to generate its Load event
- Change the file as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Algebra2
{
public partial class Algebra : Form
{
HashSet<int> A;
public Algebra()
{
InitializeComponent();
}
private void Algebra_Load(object sender, EventArgs e)
{
A = new HashSet<int>();
}
}
}
- Return to the form
Fundamental Operations on a Set
|
|
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:
In programming, to create a set, after declaring a
HashSet variable, we can call its Add()
method whose syntax is:
public bool Add(T item);
Here are examples of calling this method:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
names.Add("Kevin");
names.Add("Jane");
names.Add("Daniel");
names.Add("Ruth");
names.Add("Paul");
foreach (string str in names)
lbxNames.Items.Add(str);
}
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 the
compiler does not throw an exception
The following code will illustrate:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
if (names.Add("Kevin") == true)
MessageBox.Show("Kevin has been added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Kevin was not added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (names.Add("Jane") == true)
MessageBox.Show("Jane has been added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Jane was not added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (names.Add("Daniel") == true)
MessageBox.Show("Daniel has been added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Daniel was not added to the list");
if (names.Add("Ruth") == true)
MessageBox.Show("Ruth has been added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Ruth was not added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (names.Add("Jane") == true)
MessageBox.Show("Jane has been added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Jane was not added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (names.Add("Paul") == true)
MessageBox.Show("Paul has been added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Paul was not added to the list.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (string str in names)
lbxNames.Items.Add(str);
}
Topic
Applied: Populating a Set
|
|
- On the form, double-click the Add button
- Implement its event as follows:
private void btnAddNumbers_Click(object sender, EventArgs e)
{
int x = 0;
try
{
x = int.Parse(txtNumber.Text);
A.Add(x);
}
catch (FormatException)
{
MessageBox.Show("The value you typed is not a valid number.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
lbxNumbers.Items.Clear();
foreach (int element in A)
lbxNumbers.Items.Add(element);
txtNumber.Text = "";
txtNumber.Focus();
}
- Press Ctrl + F5 to execute
- Type a number in the text box and click Add
- Do that a few times
- Close the form and return to your programming environment
The Number of Items in a Set
|
|
In algebra, when it comes to the number of elements in
a set, there are two categories. A set is referred to as finite 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
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:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class SetHasher : Form
{
Label lblCount;
ListBox lbxNames;
public SetHasher()
{
InitializeComponent();
}
private void InitializeComponent()
{
lbxNames = new ListBox();
lbxNames.Location = new System.Drawing.Point(12, 12);
lbxNames.Size = new System.Drawing.Size(120, 95);
lblCount = new Label();
lblCount.AutoSize = true;
lblCount.Location = new System.Drawing.Point(12, 115);
Text = "Set Hasher";
MinimizeBox = false;
MaximizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new System.Drawing.Size(145, 140);
Load += new EventHandler(FormLoaded);
Controls.Add(lbxNames);
Controls.Add(lblCount);
}
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
names.Add("Kevin Walchild");
names.Add("Jane Overton");
names.Add("Daniel Albertson");
names.Add("Ruth Oyawale");
names.Add("Jane Ouelette");
names.Add("Paul Sullivan");
foreach (string str in names)
lbxNames.Items.Add(str);
lblCount.Text = "The list contains " + names.Count.ToString() + " items";
}
}
public class Exercise
{
[STAThread]
public static int Main()
{
Application.Run(new SetHasher());
return 0;
}
}
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:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
names.Add("Kevin Walchild");
names.Add("Jane Overton");
names.Add("Daniel Albertson");
names.Add("Ruth Oyawale");
names.Add("Jane Ouelette");
names.Add("Paul Sullivan");
foreach (string str in names)
lbxNames.Items.Add(str);
if( names.Contains("Jane Owerton") )
MessageBox.Show("The list contains Jane Owerton.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("The list doesn't contain Jane Owerton",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if( names.Contains("Jane Overton") )
MessageBox.Show("The list contains Jane Overton.",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("The list doesn't contain Jane Overton",
"Hash Set Operations",
MessageBoxButtons.OK, MessageBoxIcon.Information);
lblCount.Text = "The list contains " + names.Count.ToString() + " items";
}
|
|
In algebra, to state that an element a is not in the
set A, we write:
a ∉ A
This reads "a is not an element of set A" or "a is not
included in set A" or "Set A does not contain a". To perform this checking
in programming, you can use the NOT operator "!".
In algebra, after creating a set A, you may want to
create another set B but that contains the same elements as A. To do this,
you can simply assign the elements of A to B. This would be done as
follows:
A = { Kevin Walchild, Jane Overton, Daniel Albertson,
Ruth Oyawale, Jane Ouelette, Paul Sullivan }
B = A
Now, sets A and B have the same elements:
To support this operation, the HashSet
class provides another constructor (in C++ and Pascal, we could use a copy
constructor) whose syntax is:
public HashSet(IEnumerable<T> collection);
This constructor allows you to create a new set that
is made of values from an existing set. You create the new list by passing
the name of the existing list to this constructor. Here is an example:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
names.Add("Kevin Walchild");
names.Add("Jane Overton");
names.Add("Daniel Albertson");
names.Add("Ruth Oyawale");
names.Add("Jane Ouelette");
names.Add("Paul Sullivan");
HashSet<string> people = new HashSet<string>(names);
}
Once you have created the list, you can manipulate it
as you see fit. For example, you can add new items to it. Here is an
example:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class SetHasher : Form
{
Label lblCount;
Label lblPeople;
ListBox lbxNames;
ListBox lbxPeople;
public SetHasher()
{
InitializeComponent();
}
private void InitializeComponent()
{
lbxNames = new ListBox();
lbxNames.Location = new System.Drawing.Point(12, 12);
lbxNames.Size = new System.Drawing.Size(120, 120);
lbxPeople = new ListBox();
lbxPeople.Location = new System.Drawing.Point(138, 12);
lbxPeople.Size = new System.Drawing.Size(120, 120);
lblCount = new Label();
lblCount.AutoSize = true;
lblCount.Location = new System.Drawing.Point(12, 126);
lblPeople = new Label();
lblPeople.AutoSize = true;
lblPeople.Location = new System.Drawing.Point(138, 126);
Text = "Set Hasher";
MinimizeBox = false;
MaximizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new System.Drawing.Size(270, 150);
Load += new EventHandler(FormLoaded);
Controls.Add(lbxNames);
Controls.Add(lbxPeople);
Controls.Add(lblCount);
Controls.Add(lblPeople);
}
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
names.Add("Kevin Walchild");
names.Add("Jane Overton");
names.Add("Daniel Albertson");
names.Add("Ruth Oyawale");
names.Add("Jane Ouelette");
names.Add("Paul Sullivan");
foreach (string str in names)
lbxNames.Items.Add(str);
lblCount.Text = "Number of items: " + names.Count.ToString();
HashSet<string> people = new HashSet<string>(names);
people.Add("Antoinette Belton");
people.Add("Joshua Melmann");
foreach (string str in people)
lbxPeople.Items.Add(str);
lblPeople.Text = "Number of items: " + people.Count.ToString();
}
}
public class Exercise
{
[STAThread]
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SetHasher());
return 0;
}
}
Topic
Applied: Duplicating a Set
|
|
- Change the design of the form as follows:
|
Control |
Text |
Name |
Label |
|
Number: |
|
TextBox |
|
|
txtNumberA |
Button |
|
Add |
btnAddNumberA |
ListBox |
|
|
lbxNumbersA |
Button |
|
Duplicate |
btnDuplicate |
ListBox |
|
|
lbxNumbersB |
|
- Double-click the Duplicate button
- Change the file as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Algebra2
{
public partial class Algebra : Form
{
HashSet<int> A;
HashSet<int> B;
public Algebra()
{
InitializeComponent();
}
private void Algebra_Load(object sender, EventArgs e)
{
A = new HashSet<int>();
B = new HashSet<int>();
}
private void btnAddNumberA_Click(object sender, EventArgs e)
{
int x = 0;
try
{
x = int.Parse(txtNumberA.Text);
A.Add(x);
}
catch (FormatException)
{
MessageBox.Show("The value you typed is not a valid number.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
lbxNumbersA.Items.Clear();
foreach (int element in A)
lbxNumbersA.Items.Add(element);
txtNumberA.Text = "";
txtNumberA.Focus();
}
private void btnDuplicate_Click(object sender, EventArgs e)
{
B = A;
foreach (int element in B)
lbxNumbersB.Items.Add(element);
}
}
}
- To execute, press Ctrl + F5
- Add a few numbers to the left list box. Here is an example:
- Click Duplicate
- Close the form and return to your programming environment
Imagine you have two sets A and B defined as follows:
A = { Kevin Walchild, Jane Overton, Daniel Albertson, Jane Ouelette }
B = { Daniel Albertson, Kevin Walchild, Jane Ouelette, Jane Overton }
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:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> names = new HashSet<string>();
names.Add("Kevin Walchild");
names.Add("Jane Overton");
names.Add("Daniel Albertson");
names.Add("Ruth Oyawale");
names.Add("Jane Ouelette");
names.Add("Paul Sullivan");
foreach (string str in names)
lbxNames.Items.Add(str);
lblCount.Text = "Number of items: " + names.Count.ToString();
HashSet<string> people = new HashSet<string>(names);
if (people.SetEquals(names) == true)
MessageBox.Show("Both collections are the same.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("These collections are different.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
people.Add("Antoinette Belton");
people.Add("Joshua Melmann");
if (people.SetEquals(names) == true)
MessageBox.Show("Both collections are the same.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("These collections are different.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (string str in people)
lbxPeople.Items.Add(str);
lblPeople.Text = "Number of items: " + people.Count.ToString();
}
|
|
Topic
Applied: Comparing For Equality
|
|
- Add a button between the list boxes as follows:
|
Control |
Text |
Name |
Button |
|
Compare |
btnCompare |
|
- Double-click the Compare button
- Implement the event as follows:
private void btnCompare_Click(object sender, EventArgs e)
{
if (A.SetEquals(B))
MessageBox.Show("A is equal to B",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("A is different from B",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
- Return to the form
- To execute, press Ctrl + F5
- Add a few numbers to the left list box. Here is an example:
- Click Compare
- Click OK
- Click Duplicate
- Click Compare
- Click OK
- Close the form and return to your programming environment
Imagine you have two sets A and B defined as follows:
A = { Kevin, Jane, Daniel }
B = { Raul, Kevin, Tiffany, Daniel, Michael, Jane, Paul }
Notice that all members of A are also members of B. In
algebra, to express this, we write:
A ⊂ B
This is the same as:
{ Kevin, Jane, Daniel } ⊂ { Raul, Kevin, Tiffany, Daniel, Michael, Jane, Paul }
We say that A is a subset of B. This can be visually
illustrated as follows:
In algebra, any set is a subset of itself. Also, since
the empty set doesn't have any element, the empty set is a subset of any
set. We write it as:
∅ ⊂ A
and
∅ ⊂ B
An empty set is also a subset of itself.
To let you find out you whether one set is a subset of
another, the HashSet class provides the IsSubsetOf() method.
Its syntax is:
public bool IsSubsetOf(IEnumerable<T> other);
If the set passed as argument is a subset of the
variable that called the method, it returns true. Otherwise, it returns
false. Here is an example of calling this method:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> Persons = new HashSet<string>();
Persons.Add("Kevin Walchild");
Persons.Add("Jane Overton");
Persons.Add("Daniel Albertson");
HashSet<string> People = new HashSet<string>();
People.Add("Raul Fitzgerald");
People.Add("Kevin Walchild");
People.Add("Tiffany Morales");
People.Add("Daniel Albertson");
People.Add("Michael Rhodia");
People.Add("Ruth Oyawale");
People.Add("Jane Overton");
People.Add("Paul Sullivan");
if (Persons.IsSubsetOf(People))
MessageBox.Show("The Persons set is a subset of the People set.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("The Persons set is not a subset of the People set.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (string str in Persons)
lbxNames.Items.Add(str);
lblCount.Text = "Number of items: " + Persons.Count.ToString();
foreach (string str in People)
lbxPeople.Items.Add(str);
lblPeople.Text = "Number of items: " + People.Count.ToString();
}
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.
Topic
Applied: Checking for Sub-Set
|
|
- Add another label to the form and change its Text to Number
- Add a second text box and another button to the form
- Change the design of the form as follows:
|
Control |
Text |
Name |
Label |
|
Number: |
|
TextBox |
|
|
txtNumberA |
Button |
|
Add |
btnAddNumberA |
Label |
|
Number: |
|
TextBox |
|
|
txtNumberB |
Button |
|
Add |
btnAddNumberB |
|
- On the form, double-click the second Add button
- Implement its event as follows:
private void btnAddNumberB_Click(object sender, EventArgs e)
{
int x = 0;
try
{
x = int.Parse(txtNumberB.Text);
B.Add(x);
}
catch (FormatException)
{
MessageBox.Show("The value you typed is not a valid number.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
lbxNumbersB.Items.Clear();
foreach (int element in B)
lbxNumbersB.Items.Add(element);
txtNumberB.Text = "";
txtNumberB.Focus();
}
- Change the code of the Compare button as follows:
private void btnCompare_Click(object sender, EventArgs e)
{
if (A.SetEquals(B))
MessageBox.Show("A is equal to B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("A is different from B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if( A.IsSubsetOf(B))
MessageBox.Show("A is a sub-set of B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("A is not a sub-set of B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
- To execute, press Ctrl + F5
- Add a few numbers to the left list box
- Add the same numbers to the right list box in any order of your
choice
- Add a few more numbers to the right list. Here is an example:
- Click Compare
- Click OK
- Close the form and return to your programming environment
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:
On the other hand, 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 property 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 the
IsProperSubsetOf() method. Its syntax is:
public bool IsProperSubsetOf(IEnumerable<T> other);
This method takes a HashSet value as
arguments 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:
private void FormLoaded(object sender, EventArgs ea)
{
HashSet<string> Persons = new HashSet<string>();
Persons.Add("Kevin Walchild");
Persons.Add("Frank Maguire");
Persons.Add("Jane Overton");
Persons.Add("Daniel Albertson");
HashSet<string> Students = new HashSet<string>();
Students.Add("Jane Overton");
Students.Add("Kevin Walchild");
Students.Add("Daniel Albertson");
Students.Add("Frank Maguire");
HashSet<string> People = new HashSet<string>();
People.Add("Raul Fitzgerald");
People.Add("Kevin Walchild");
People.Add("Tiffany Morales");
People.Add("Daniel Albertson");
People.Add("Michael Rhodia");
People.Add("Frank Maguire");
People.Add("Ruth Oyawale");
People.Add("Jane Overton");
People.Add("Paul Sullivan");
if (Persons.IsProperSubsetOf(Students))
MessageBox.Show("The Persons set is a proper subset of the Students set.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("The Persons set is not a proper subset of the Students set.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (Persons.IsProperSubsetOf(People))
MessageBox.Show("The Persons set is a proper subset of the People set.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("The Persons set is not a proper subset of the People set.",
"Collection Class",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Topic
Applied: Checking for Proper Sub-Set
|
|
- Change the code of the Compare button as follows:
private void btnCompare_Click(object sender, EventArgs e)
{
if (A.SetEquals(B))
MessageBox.Show("A is equal to B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("A is different from B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if( A.IsSubsetOf(B))
MessageBox.Show("A is a sub-set of B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("A is not a sub-set of B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if( A.IsProperSubsetOf(B))
MessageBox.Show("A is a proper sub-set of B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("A is not a proper sub-set of B.",
"Algebra of Sets",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
- To execute, press Ctrl + F5
- Add a few numbers to the left list box
- Add the same numbers for the right list box in any order of your
choice
- Click Compare
- Click OK
- Add a few more numbers to the right list
- Click Compare. Here is an example:
- Click OK
- Close the form and return to your programming environment