Resurrection of Delphi

As a dilligent subscriber of DotnetPro-Magazine (monthly developer magazine in German) I read in the last issue about the reincarnation of Delphi and Object Pascal undet the .Net platform. Before .Net Delphi was the language I had had most experience with, spending days and weeks trying to develop my own visual components and to develop something worth to show the others. Delphi is still the IDE of choice if you need to programm something, which should be compiled into the native code and should run on a variety of Windows Systems, starting from Win98SE on. In my last project where I had to programm the software which controls acoustic modem in marine research I had to use it again (in 2009, imagine!) in order to make sure that the final code will run on both old and new laptops they have.

Sets in .Net and C#

One of the articles of the actual issue was about "mimicking Object Pascal sets under .Net" by Bernd Klaiber, where he describes the class, developed by him in order to get a behaviour similar to what is called "sets" in Delphi. I was a little bit surprised to read about this implementation and even recommendation to use this "generic implementation with overloaded operators" implementation, which is somehow compared to the classical C# Flags. This is, a propos, the only point I agree -- the implemenation offers much more functionality compared to Flags, but do we really need this functionality?

Classical implementation from 2002

First of all there is a classical and well-known implementation of sets under .Net, which can be found and downloaded from CodePage. The implementation (and the article in the CodePage portal which supports it) was published as early as in 2002 and was revised in 2004, it implements a whole hierarchy of Set classes, implementing not only the classical set, but also dictionary and hash sets. The implementation does not overload any operators, probably because it was mainly implemented before this became possible in .Net (even though it is implemented as a generic class in the latest version available on CodeProject).

Drawbacks and what we actually need

I used this implementation in one of my projects at work and found it not so handy, because you have to deploy 2 separate DLLs together with your code (this is just one stand-alone DLL), even if you use just the smallest part of the overall functionality. By the way, when I think of sets, I basically think of the following features: ability to have a set of data where I can add to and remove from and check whether a certain element belongs to the set of not. Other operations like union or interseption are nice, but if I need them I rather use something in the direction of SQL-server or SQL-provider to implement this feature. That's why basically speaking of sets we need three operations:

  • Add something to set.
  • Remove something from the set.
  • Check, if something belongs to the set or not.

Flags

For static values one should take the classical C# flags, which are nothing more than a binary enum with possibility to check for certain bit. Here is just a small extract from my C# workshop demo project to illustrate the issue:


[Flags]
enum DaysOfWeek : byte
   { Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8,
         Friday = 16, Saturday = 32, Sunday = 64 }

public static void FlagsTest()
{
   DaysOfWeek workingDays = DaysOfWeek.Monday | DaysOfWeek.Tuesday
                 | DaysOfWeek.Thursday | DaysOfWeek.Wednesday | DaysOfWeek.Friday;
   DaysOfWeek today = DaysOfWeek.Monday; 
   DaysOfWeek day1 = today;
   day1 |= DaysOfWeek.Sunday; // adding new day
   day1 |= DaysOfWeek.Tuesday; // adding another day
   day1 &= ~DaysOfWeek.Saturday; // removing one day
   if ((today & workingDays) != 0)
     { Console.WriteLine("Today is " + today + ", working day"); }
   else
     { Console.WriteLine("Today is " + today + ", holiday"); }
 }

What if we need more?

To some reason unknown to me there are quite a few who know that there is actually a full-fledged set class in .Net starting from the version 3.5. This is the hashset class, which implements all methods we need in order to manipulate elements and check for them. It supports .Add(), .Remove() (together with .Clear()) and .Contains(), the basic 3 operations I ever needed from a set. Besides, it also supports many real set operators like .ExceptWith() or .Overlaps(), which were so arduously implemented in the article.

Then I switched to this class in a newer implementation of my project at work the total project sized schrinked from about 800Kb down to 16Kb (imagine!) and I didn't have to deploy two separate DLLs with my project anymore (that was great!). The only method which I missed a lot was the .AddAll(T[] T) methods, which woud allow me to add the whole array of elements to the set at once, without implementing a loop. That's why I just briefly concocted the following class:


    class SetOfStrings : HashSet <string>
    {
        public bool AddAll(string[] newItems)
        {
            try
            {
                foreach (string newitem in newItems)
                {
                    base.Add(newitem);
                }

            }
            catch (Exception)
            {
                return false
            }
            return true;

        }
    }

Alas, this new class was not generic anymore (I did not care for it because I my set was hard typed), but it implemented the functionality I needed. Apparently it was the only difference between the CodeProject class and the hashset, since the rest of the code just worked without further correction. Now I use this code in several project I work at and I am fully satisfied with the execution speed and flexibility.