The warm fuzzy blanket of type safety.

Published 2.23.2006 by ~mattg

Since most of my professional career has been .NET (VB and C#), typed collections have been something that I implement through one general collection or another (some of my favorites include ArrayList and CollectionBase).  Now that we’ve switched over to .NET 2.0, I’ve been introduced to the wonderful world of C# generics and the type safety they provide.

What’s so great about that?  Well, as I’ve come to find out, anyone working in C++ has enjoyed templates for some time. While they are not the same as generics, they provide the ability to say “make me a list of strings”.  To the C++ coder, this doesn’t seem like a big deal.  However, in C#, if you want a dynamically sized list of strings, you have to use an ArrayList (which holds objects) and then, whenever you retrieve the data, convert the object to a string.  While strings may not be that difficult, when you start moving into custom classes, it gets to be a pain.

With generics, you can define a dynamic list of any class you like.  The list will only accept an instance of the class you defined, and during retrieval, casting the class is not necessary.  It makes the code much easier to manage.

So while the rest of the world says “generics? No biggie,” I’m snuggled in the warm fuzzy blanket of type safety.

Filed under .NET Development

Comments (1)

Comments RSS - Trackback - Write Comment

  1. Kevin Dangoor says:

    I greatly perform the warm fuzzy blanket of unit tests. My last work in Java was in Java 1.4, so I was really looking forward to the 1.5 generics. They do make life better when you’re stuck in a static language. But, you still need tests… and, if you’re writing tests, you might as well get the benefit and joy of a dynamic language.

    Of course, I really shouldn’t have posted this comment, given that static typing vs. dynamic typing is one of these things rooted in opinion (much like emacs vs. vi). Oh well, too late now :)

    Posted 3.21.2006 @ 10:20

Write Comment