Start New Discussion within our Software Development Community

For anyone who likes to do something with sets in C#, this can be a starting point.
The full project (VS 2010) is also included. Have fun with programming!

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HashSetTest
{
    public partial class Form1 : Form
    {
        //Initialize new instances of the HashSet<T> class that are empty and use the default equality comparer for the set type.
        HashSet<int> Set1 = new HashSet<int>();
        HashSet<int> Set2 = new HashSet<int>();
        int SetElement;

        public Form1()
        {
            InitializeComponent();
        }

        private void Set1Txb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
            {
                // convert text of textbox to int, no err checking!
                SetElement = int.Parse(Set1Txb.Text);
                // add to Set1
                Set1.Add(SetElement);
                // update label in form
                Set1Lbl.Text = Set1Lbl.Text + " " + Set1Txb.Text;
                // remove text
                Set1Txb.Text = "";
            }
        }

        private void Set2Txb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
            {
                // convert text of textbox to int, no err checking!
                SetElement = int.Parse(Set2Txb.Text);
                // add to Set2
                Set2.Add(SetElement);
                // update label in form
                Set2Lbl.Text = Set2Lbl.Text + " " + Set2Txb.Text;
                // remove text
                Set2Txb.Text = "";
            }
        }

        private void UnionBtn_Click(object sender, EventArgs e)
        {
            // Create new set containing all elements of Set1
            HashSet<int> TheUnion = new HashSet<int>(Set1);
            // Create union with Set2
            TheUnion.UnionWith(Set2);
            ShowSet(TheUnion);
        }

        private void InterSectBtn_Click(object sender, EventArgs e)
        {
            // Create new set containing all elements of Set1
            HashSet<int> TheIntersection = new HashSet<int>(Set1);
            // Create intersection with Set2
            TheIntersection.IntersectWith(Set2);
            ShowSet(TheIntersection);
        }
        
        private void ShowSet(HashSet<int> S)
        {
            string str = "This set contains: ";

            foreach (int element in S)
            {
                str += element.ToString() + " ";
            }
            MessageBox.Show(str);
        }
    }
}
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.