Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A Set class

4.48/5 (11 votes)
20 Nov 2006CPOL3 min read 1   363  
A Set class using a System.Collections.Generic.Dictionary to hold its elements.

Introduction

This class implements a Set using a System.Collections.Generic.Dictionary to hold the elements.

Using the code

The zip file contains three C# files:

  • Set.cs contains the Set class definition.
  • GetTypes.cs contains the static method to get an array of types.
  • SortMode.cs contains the definition of the SortMode enumeration.

To use them, simply add them to your project. The following section describes how to use the class.

Empty instances of Set can be constructed with:

C#
Set<int> a = new Set<int>() ;
Set<char> b = new Set<char>() ;
Set<string> c = new Set<string>() ;

You may construct a Set to contain whatever type of items you require (limited only by the underlying System.Collections.Generic.Dictionary).

C#
Set<object> s = new Set<object>() ;
Set<int[]> s = new Set<int[]>() ;
Set<System.Data.DataRow> s = new Set<System.Data.DataRow>() ;
Set<System.EventHandler> s = new Set<System.EventHandler>() ;
Set<System.Collections.IEnumerable> s = 
       new Set<System.Collections.IEnumerable>() ;

Instances of Set can be constructed and initialized with:

C#
Set<int> s = new Set<int> ( 1 , 2 , 3 ) ;
Set<char> s = new Set<char> ( '1' , '2' , '3' ) ;
Set<string> s = new Set<string> ( "One" , "Two" , "Three" ) ;

Set<int> s = new Set<int> ( somearrayofints ) ;
Set<char> s = new Set<char> ( somearrayofchars ) ;
Set<string> s = new Set<string> ( somearrayofstrings ) ;

Set<int> s = somearrayofints ;
Set<char> s = somearrayofchars ;
Set<string> s = somearrayofstrings ;

The constructor can take any object of type object, but the converters* are somewhat more limited because converting from object or an interface is not allowed.

When an item that is not of the specified type is encountered, it is checked to see if it is IEnumerable, and if so, it will be foreached to recurse across it. This is how the arrays are handled in the above examples. If an object that is neither the target type nor IEnumerable is encountered** an InvalidOperationException is thrown.

Additional converters may be added as needed. And I'll point out that the class is partial so you may put any additional code in a separate file and not modify mine.

* Yes, I realize that having these conversions as implicit goes against the guidelines (because they could fail), but hey, it's my code and I'll do what I want. If you want them to be explicit, define explicit.

** Null items are ignored by default. If throwing NullReferenceExceptions for nulls is desired, define ThrowOnNull.

Once you've constructed instances, you may perform set operations on them:

| or + Union; { 1 , 2 , 3 } | { 2 , 4 , 6 } = { 1 , 2 , 3 , 4 , 6 }.

C#
a = b | c ;
a = b + c ;
a |= b ;
a += b ;

& Intersection; { 1 , 2 , 3 } & { 2 , 4 , 6 } = { 2 }.

C#
a = b & c ;
a &= b ;

- Relative complement; { 1 , 2 , 3 } - { 2 , 4 , 6 } = { 1 , 3 }.

C#
a = b - c ;
a -= b ;

== Equality.

C#
if ( a == b ) { ... }

!= Inequality.

C#
if ( a != b ) { ... }

<= Subset.

C#
if ( a <= b ) { ... }

< Subset but not equal.

C#
if ( a < b ) { ... }

>= Superset.

C#
if ( a >= b ) { ... }

> Superset but not equal.

C#
if ( a > b ) { ... }

The following public methods and properties are also available

Add ( params object[] Items ) attempts to add the items to the Set. In some cases, this is more efficient than using the Union operator.

C#
s.Add ( 1 ) ;
s.Add ( 1 , 2 ... ) ;
s.Add ( somearrayofints ) ;

Remove ( params object[] Items ) attempts to remove the items from the Set. In some cases, this is more efficient than using the relative complement operator.

C#
s.Remove ( 1 ) ;
s.Remove ( 1 , 2 ... ) ;
s.Remove ( somearrayofints ) ;

Contains ( params object[] Items ) returns true if the Set contains the item(s), otherwise false. In some cases, this is more efficient than using the subset operator.

C#
if ( s.Contains ( 1 ) ) { ... }
if ( s.Contains ( 1 , 2 ... ) ) { ... }
if ( s.Contains ( somearrayofints ) ) { ... }

Clear() removes all elements from the Set.

C#
s.Clear() ;

Cardinality is the number of elements in the Set.

C#
if ( s.Cardinality > 0 ) { ... }

EqualityComparer gets/sets the EqualityComparer of the underlying System.Collections.Generic.Dictionary.

C#
Set<string> s = new Set<string> ( "abc" , "ABC" ) ;
System.Console.WriteLine ( s.EqualityComparer.ToString() ) ;
System.Console.WriteLine ( s.ToString() ) ;
s.EqualityComparer = System.StringComparer.CurrentCultureIgnoreCase ;
System.Console.WriteLine ( s.EqualityComparer.ToString() ) ;
System.Console.WriteLine ( s.ToString() ) ;

Yields:

System.Collections.Generic.GenericEqualityComparer`1[System.String]
{ abc , ABC }
System.CultureAwareComparer
{ abc }

ToString() traverses the Set performing ToString() on each element and concatenating the resultant strings together in proper Set format: { element1 , element2... }.

C#
System.Console.Write ( s.ToString() ) ;

ToString ( SortMode SortMode , params object[] FormatInfo ) traverses the Set performing ToString ( FormatInfo ) on each element and concatenating the resultant strings together in proper Set format: { element1 , element2... }.

C#
Set<int> s = new Set<int> ( 20 , 3 , 100 ) ;

System.Console.WriteLine ( s.ToString ( SortMode.None   ) ) ;
System.Console.WriteLine ( s.ToString ( SortMode.Native ) ) ;
System.Console.WriteLine ( s.ToString ( SortMode.String ) ) ;
System.Console.WriteLine ( s.ToString ( SortMode.None   , "000" ) ) ;
System.Console.WriteLine ( s.ToString ( SortMode.Native , "000" ) ) ;
System.Console.WriteLine ( s.ToString ( SortMode.String , "000" ) ) ;

Yields:

{ 20 , 3 , 100 }
{ 3 , 20 , 100 }
{ 100 , 20 , 3 }
{ 020 , 003 , 100 }
{ 003 , 020 , 100 }
{ 003 , 020 , 100 }

Interfaces

IEnumerable GetEnumerator returns the GetEnumerator of the underlying System.Collections.Generic.Dictionary's Keys property.

C#
foreach ( int i in s ) { ... }

History

  • First submitted - 2006-11-14.
  • Updated 2006-11-15.
    • Added the ability to specify the EqualityComparer by using a Dictionary rather than a List.
    • Added the ability to specify formatting information to the ToString().

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)