Mark Mintoff My superpower is common sense


Extended Classes

Oft it is, that whilst coding I think to myself: "Self, wouldn't it just be awesome if this class I am making use of was simpler to use?" And the answer is always a definite yes. Hence, I will share with you four class "extensions" I developed, in order to make mgt;y life (well, my programming life) just a tad bit easier. These are:
  1. WeakReference with support for generics
  2. Dictionary which is easier to use
  3. Pair with support for generics
  4. Triplet with support for generics

WeakReference<T> The WeakReference class is one I have made extensive use of, in a complex Caching system which I developed, but immediately upon use, my first thought was that it needed to make use of Generics, as all the boxing and unboxing from an object into the type I want is colossally tedious. Hence, I developed this: Making use of this class allows for the exact same functionality as the original WeakReference, with the added benefit of having Typed parameters.
ExtendedDictionary<TKey, TValue> The Dictionary is easily my all-time favorite collection, however, I personally hate all the responsibility associated with checking ContainsKey all the time, and hence opted to create a Dictionary Wrapper in order to deal with this troublesome problem: This extension of the Dictionary, means that you no longer have to check for ContainsKey when attempting to extract a value from the Dictionary. You no longer have to check for ContainsKey when setting a value in the Dictionary. becomes and now becomes simply: Use it wisely. I don't want to be responsible for any gigantic logical faults.
Pair<A, B> and Triplet<A, B, C> System.Web introduces to us the Pair and Triplet objects, which at first glance are absolutely brilliant. But then again, they have no generics support in-built. In addition to that, for some strange reason the Pair and Triplet classes exist in System.Web. I don't much fancy adding a reference to System.Web in Class Libraries where that reference does not belong, so on to making my own: and The concept here is pretty simple; classes which contain 2 or 3 values respectively. The override of Equals and GetHashCode is necessary if you have any intention of using Pair or Triplet as a key in a Dictionary.
That concludes my outlining of four extended classes out of the many extensions and helper methods I have created to make my life slightly easier. I trust you will use them well and I hope they will bring you the same added simplicity to coding that they brought to me.
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
Comments (8) Trackbacks (0)
  1. The famous Pair and Triplet! Use them till today. And the ingenious Dictionary extension makes me want to use it just for the heck of it!

    • I would at least try using the ExtendedDictionary if I were you; I find it to be brilliantly comfortable and always find myself using ExtendedDictionary over Dictionarywhenever a Dictionary is needed.

  2. As regards the Pair and Triplet classes, you might want to use the Tuple class introduced in .NET 4:

    It allows for tuples with up to 8 generic values, which are then accessed using strongly-typed properties appropriately named Item1, Item2 etc…

    • Unfortunately I have not had the chance to start using .Net 4 yet, so I didn’t even know about this. However, from the looks of things, since it generates a “var” I’m not sure whether it can be used as a Dictionary Key.

      With my Pair<A> and Triplet<A> classes, I can create a 2 or 3 part key for Dictionaries.
      Can this be achieved using Tuple?

      • What do you mean exactly by “producing a var” ?

        In C#, the “var” keyword is just used for type inference.

        The example in the MSDN page can be written as:

        Tuple<int,int,int,int,int,int,int,Tuple> primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);

        They’re exactly the same thing.

        • Ah right, well that’s pretty good then.
          Again, I have not had access to .Net 4.0 as of yet, so let me just ask this:

          Can you create an instance through constructor, rather than use Tuple.Create?

          I might just try and replicate this functionality myself in .Net 3.0, you see.

      • Here’s a good example of recreating such:

        And no, you can’t create a Tuple with a ctor because the Tuple class is abstract

        • According to that example (in .Net3.0 at least), in order to be able to compare two objects of type Tuple with the same values within them, you need to override Equals and GetHashCode.

          If you do not, the comparison will always fail. With that implementation you posted, due to the base class chaining, you need to implement Equals and GetHashCode on all of them, since they all have a different amount of properties.

          Equals and GetHashCode are necessary if you would hope to use the object as a key in a Dictionary. I stress this as important; I have an implementation of multi-language whereby I make use of “PropertyName”, “LanguageID” and “EntityID” as my key; hence my implementation of the Triplet<A,B,C> class (which includes the Equals and GetHashCode implementation).

Cancel reply

No trackbacks yet.