views:

241

answers:

6

I am migrating a 1.1 winforms app to 2.0. what are the main things i should immediately change because of generics. Here what i have so far:

  1. Replace all hashtables with generic dictionaries
  2. Replace all arraylists with List<>
  3. Replace all CollectionBase derive classes with : List<>

Any others that should be done immediately?

thks, ak

+6  A: 

Any others that should be done immediately?

Generally, change any mention of IEnumerable to IEnumerable<T>, where possible. Migration can be greatly helped by switching the whole namespace, i.e. un-importing System.Collections in every file and instead importing System.Collections.Generic.

Also, search for mentions of object and/or usage of boxing in your code and consider whether this is still appropriate or should be replaced by generics.

As jalf reminded me in the comments, another important change is the switch to the generic version of IComparable where applicable.

Konrad Rudolph
And similarly, IComparable with IComparable<T>
jalf
@jalf, thank you, completely forgot about that.
Konrad Rudolph
+3  A: 

Generic collections are definitely preferred due to their expressiveness. One thing to keep in mind if changing from the non-generic collections is that sometimes the behavior may be different than you expect. For example, using the indexer on a Hashtable vs. a Dictionary will act differently for values that are not present. Hashtable will return null while Dictionary will throw.

Hashtable ht = new Hashtable();
ht.Add(1, "one");
string s1 = ht[1;  // s1="one"
string s2 = ht[2]; // s2=null

var dic = new Dictionary<int, string>();
dic.Add(1, "one");
string s1 = dic[1];  // s1="one"
string s2 = dic[2];  // throws KeyNotFoundException

A common way to handle this is to use the following technique:

string s = null;
if (dic.TryGetValue(k, out s))
{
    // if we're here, k was found in the dictionary
}

This will show up only at runtime so it's worth knowing ahead of time.

dpp
+8  A: 

I don't think anything should be done immediately! The 1.1 code works, right? What's the business case for the wholesale swap to generics? Compile the app under 2.0, get it running and tested. And then, as new features are needed that would let you exploit generics well, implement those features as generics.

rp
This is what I was thinking when reading the question. Don't change anything if you don't have to. When you go to write new code, start using generics, or if you are doing changes to existing code, then switch to generics. But don't try to change everything all at once.
Kibbee
+2  A: 

See Bill Wagner's new book More Effective C#. There are a lot of great tips for moving over to generics.

Brian Genisio
A: 

I wouldn't recommend using List<T> instead of CollectionBase. Instead, Collection<T> gives you comparable overrides.

Thomas G. Mayfield
A: 

unless you already have a suite of unit tests with excellent code coverage, don't change anything unnecessarily

otherwise you are just asking for trouble, not to mention inventing busywork...

Steven A. Lowe