« Christmas at the Bergers' | Main | reflector and a good quote »
December 09, 2003
generics in C# 2.0
A very long article but the beginning is pretty good at describing why generics are needed.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/csharp_generics.asp
"Generics allow you to define type-safe data structures, without committing to actual data types."
"However, there are two problems with Object-based solutions. The first issue is performance. When using value types, you have to box them in order to push and store them, and unbox the value types when popping them off the stack. Boxing and unboxing incurs a significant performance penalty in their own right, but it also increases the pressure on the managed heap, resulting in more garbage collections, which is not great for performance either. Even when using reference types instead of value types, there is still a performance penalty because you have to cast from an Object to the actual type you interact with and incur the casting cost:"
"The second (and often more severe) problem with the Object-based solution is type safety. Because the compiler lets you cast anything to and from Object, you lose compile-time type safety."
So if I'm thinking about this right and there is a generic version of ArrayList, I could do something like this in SWOOSH where I have a class called Stat:
ArrayListallStats = new ArrayList (); //tell the ArrayList to only work with Stats allStats.Add(myStat);
Stat newStat = allStats[0]; //no casting
Posted by mikel at December 9, 2003 01:34 PM
Comments
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)