Chris said:
The next version of VS.Net (Whidbey) promises to add generics support to
VB. I have a vague remembrance of Templates in C++, but I guess I need a
refresher.
What will generics allow us to do? How do they make coding easier?
Is there a resource that will give me a basic understanding of what
generics are and how generics will be useful (in the context of VB.Net)?
Thanks
Chris
One of the many benifits will be in the use of collections - especially
with value types. Currently, all of the standard collection types -
ArrayList, Hashtable, etc. - all take object as the type stored. What
this means is that all values stored in these collections are cast to
type object when stored, and then you have to cast back to their
original type when used. This is especially expensive for value types,
because of the overhead of boxing/unboxing the values. It also produces
less readable code, and more of it...
With generics, you'll be able to specify the type of the collection.
I'm not exactly sure what the VB.NET syntax will be, but it might look
something like:
Dim myList As New ArrayList<Integer>()
This would have the affect of creating an array list that would only
accept Integer values and be optimized for value types. I have seen
articles on another group that suggested that they are seeing about a
30% performance increase for value types and like 10% for reference
types. I can't find the article, so don't hold me to those numbers
This will also work with custom types...
Dim myList As New ArrayList<MyCustomClass>()
myList.Add(New MyCustomClass("Tom"))
....
Consoel.WriteLine(myList(0).Name)
Anyway, you get typesafty, less code, and less overhead... Generics are
a good thing.
Tom Shelton