C# 2.0 Feature: Generics


What are “generics”? Well, if you’ve ever used C++, then you’ve probably ran across some code that looked like this:

std::stack myIntStack;

If you have seen this before, then you have a good foundation for understanding generics because the in the statement above is called a “template” which is a very simple version of generics. If you haven’t code like that before, what it is saying is that you would like to create a “stack” data structure that will hold integers. If you used instead, you would have a stack that stores floating point numbers, etc. It’s very handy because you do not have to re-write classes to handle a float one time and then an int the next.


The new version of generics will be a part of C# when Visual Studio .NET 2005 (formerly know as “Whidbey”) ships. In this version, generics are even more powerful than templates in C++. Generics in C# allow you to take a collection class that may normally accept only objects and strongly type that collection class to accept only a specific type of object. There will certainly be times when you WILL NOT want to use generics because you need to store multiple types of objects, but in most cases, you might know ahead of time that this particular collection will consist only of objects of type X. Strong typing isn’t the only benefit though. As Shreyas pointed out to me last week, the compiler can actually optimize your class based on that strong typing to give you a little bit of a performance boost too, which really makes all of this even better. Now that you know the idea behind it, lets look at an example.


In C# 1.0, if you want to build an array of Int32s that is dynamic, you might do:

 Stack myStack = new Stack();
 myStack.Push(1);
 myStack.Push(2);
 myStack.Push(3);
 return (Int32) myStack.Pop();

That’s not too bad, but lets look at the same example with C# 2.0:

 Stack<Int32> myStack = new Stack<Int32>();
 myStack.Push(1);
 myStack.Push(2);
 myStack.Push(3);
 return myStack.Pop();

You’ll notice there are not a lot of code changes here, but in the C# 1.0 version, if you push anything other than an Int32 onto that stack, and then try to pop it and cast as an Int32, you better have a try/catch block around it or an “as” statement at the end of it because you’re going to get a runtime error (or crash if you’re unlucky…actually you’ll get an InvalidCastException if you really care). With the 2.0 version, not only is the code faster (the .Push() and .Pop() execute faster) but you don’t have to worry about a runtime exception because the compiler will do the type checking for you (hence the strong typing).


I hope this gives you a little bit better idea about what type of changes are on the way to the best programming language out right now. C# is great as it stands, but generics along with the other new features (which I will blog about soon) are going to make 2.0 even easier to use and faster to execute. And in case you are wondering, IntelliSense will be smart enough to understand the generics as well so the return type of Pop in the example would actually show as Int32 in this case. Nifty stuff indeed.

Long Week

Wow, this week has been waaaay too long. I’ve been going non-stop since last weekend and now it’s nice that things are finally slowing down. I tried to find time to blog, but that just didn’t have time to even respond to many posts, much less post myself. I finally got the GMAT out of the way with an unofficial score of 620. Not the score I had hoped for, but then again, I’m very happy with it considering I only studied for it for a couple of days and I didn’t even do the full practice test. Sorry to rant again on the GMAT, but one sure fire way to get an 800 on that test is to take it as soon as you graduate from high school as you possibly can. The questions on it are not beyond simple sentence correction, basic algebra I, geometry, and a tiny bit of logic (not like discrete math logic, but general “thinking with your brain” type logic). This made it extremely hard for me, because the subjects I’m good at (like science and computer anything) didn’t have a thing to do with this test and the concepts that are on there are things that I can’t even take here at the University. Anyway, it’s just frustrating to think that I could have gotten a much much higher score had a taken that even in my freshman year of high school than I did now. Oh well, it’s over and done with now and hopefully everything will be back to normal. I’m going to post my first C# 2.0 blog now… 🙂

C# Integer Only Textbox Example

I added a little example program the other day and I just wanted to let people know it was there in case it helps someone. It’s called “Integer Only Textbox Example” and it’s written in C# (what else is there? 😉 ). It will show you how to use the keypress event of a standard WinForm TextBox to force it to accept only positive and negative integers. I wrote this as an example to help a fellow blogger on here, but hopefully it will help other people out a little bit too. Also, you should make sure you actually double check that the value in the textbox WAS an integer (e.g. don’t just assume this works 100% of the time, because although it should, you should still use try/catch around Int32.Parse(textBox.Text) just to be safe). Anyway, here it is:

http://www.thespoke.net/downloads/details.aspx?ArtifactID=c98f9853-630f-44d6-8af7-3f190d3c9cbb