Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

Justin Lovell's Blog

Mood: The Grumpy Coder

Inheritance + Generics + constraints == the worst documentation experience

How to begin one of those blog posts that is ORIGINAL!? I have been waiting ever since I set up my blog to blog something that is originally geeky. That means new code that is absolutely new. Anyway, not to brag too much and to get on about it (as actions are better than words), here it goes:

As you probably figured out by now, I have been trying out all the new things in Whidbey (I only have the Express edition so I am trying scraps of meat here and there). Alright -- I have not read a book on the C# 2.0 specifications and I can't download the bugger from the MSDN site. Besides that, I expected that someone would have at at least written a blog post or article about the problem/dilema that I ran into.

Basically I wanted to create my own generical class which:

  1. Inheritted from the List (the generical) class.
  2. Apply custom constraints.

Ok, both of them are pretty easy to do. Here is some code for inheritting from the List:

public class MyList<T> : List<T> {
   ...
}

And to accomplish the second part, to apply the custom constraint, you have some code like this:

public class MyCustomObject {
   ...
}

public class MyList<T> where T : MyCustomObject {
   ...
}

Ah! Here comes the time of merging the two together. I initially fired up my browser and went to google and did some queries on how to accomplish my task (*). Nope, no luck there. Obviously (or what I thought), it is spelt out LOUD AND CLEAR in the documentation because no one blogged about (although it is strange due to the fact that if anything is in the documentation, it has be regurgitated in someone's blog). Guess what? I could not find it.

So... the next plan of action was to fire up MSN Messenger and ask my dear friend, Teemu, if he has a solution to my problem (as it was syntaxical problem)... after all, he is known for reading a ton of programming related books (just look at the amount of reviews he does). To my surprise, he had no idea of the solution (although he hinted to me that it was probably due to him drinking -- he is on vacation). At this point, I declared that Generics needs much more documentation.

What then!? I decided to imagine myself as being the program manager at Microsoft for the C# language. What would I do if I could dictate the most popular .NET language (**)? I then tried:

public class MyList<T> where T : MyCustomObject : List<T> {
   ...
}

The bolded part made sense to me because that is where the inheritance chain should be declared. However, C# did not see it my way. I then thought about the English language -- the word "where." I then constructed the sentence in my head "if I was the flippin' Generic, and I came from Microsoft where the hell would I allow inheritance." The answer was embedded in the sentence construction:

Life is good now! It took me one hour and a bit to figure out the problem but at least I am the first one to share this new "found" information with the community.

public class MyList<T> : List<T> where T : MyCustomObject {
   ...
}

Notes

(*) - While on my search, I managed to find this great blog post about Generics. It coverred EVERYTHING but what I was looking for. Anyway, the link: Design Guidelines: Generics.

(**) - The VB.NET syntax was pretty much easy to pick-up because it is soooo simplified. I knew the inheriting generics was possible because I was able to get the VB.NET code running.

Sponsor
Published Tuesday, July 06, 2004 3:01 PM by jlovell

Comments

 

jlovell said:

I bet you want to have this book:

http://www.awprofessional.com/bookstore/product.asp?isbn=0321154916&redir=1
(The C# Programming Language)

as your guide.
July 6, 2004 3:06 PM
Anonymous comments are disabled