Hangin’ with generics

I've been using generics in a project that I'm using on Whidbey. They are tremendously
convenient when you want to use collection classes. That wasn't really very surprising.

What was surprising - mostly because I hadn't spent any time reading the generics
spec - was how useful generic methods would be. For my app, I sometimes needed to
fetch a list of values from a database. I didn't want to write a separate function
for each type,  and with generic methods, I could write the following:

public List<T> GetValues<T>(string selectStatement)
 OleDbCommand select = new OleDbCommand (selectStatement, connection);
 List<T> list = new List<T> ();
 OleDbDataReader reader = select.ExecuteReader ();  while (reader.Read())
  list.Add ((T)reader[0]);
 return list;

I think that's pretty cool. I wrote several functions like that that perform database
operations in a generic (ha ha!) way. So I think generics are going to be cooler than
I expected.

I should mention one more thing. If the type you use in the generic method is one
of the parameters, the compiler can figure out what generic type you're using, and
you don't have to specify it. If I wrote:

        public T Process<T>(T param) {...}

I could call it with:

        int i = inst.Process(12);

and the compiler figures out that I want Procees<int>.

Oh, and before you ask me for the spec, you can't have it... yet... It's still churning
due to the ECMA process, and we don't want to release it until it's a bit more concrete.

Comments (13)
  1. Omer says:

    That’s really cool! How long before us common folks could use it?

  2. Ryan says:

    So the ‘ECMA process’ can push back the release date of Whidbey, while its mainly ready for releasing ? 😀

  3. You said, "he compiler can figure out what generic type you’re using…"

    Is this a JIT compile operation or is it a C# –> IL operation. Not sure if that is the right way to pose the question; guess what I am looking for is if there will be overhead (ie will it be at runtime) in that discovery process?

  4. Good stuff 🙂 Can’t wait to get my hands on generics. I wonder how performance is on generics…

  5. SBC says:

    Could you please comment more on the ‘typecasting’ with generics, i.e., in your code: list.Add ((T)reader[0]);

  6. User says:

    Scary. Remember ATL and people running away for Java. Obviously the same mistakes are going to be made again.

  7. Kardax says:

    Everything I’ve read about .NET’s generics implementation is that the templates are expanded at runtime by the JIT. This should keep bloat to a minimum 🙂

  8. Phil says:

    Since generics will not be supported until the next release of Visual Studio, how should we emulate templates currently in C#? Clemens Vasters has a template library, but is there a recommended way to emulate generics?

  9. moo says:

    Yeah pitty about the addition of partial types and Lambada functions (anonymous methods) expect to see shity code out there once they are implemented. Thanks a bunch, NOT. Lazy programmers get the hell away from me.

  10. Philippe says:

    "generics" is that like STL for C++? Sure looks like it! Then that’s way cool, can’t wait for it!

  11. I think it is worth pointing out that casts within a generic method like this should be done with caution as the method appears strongly typed when, in fact, it buries the cast. See http://mark.michaelis.net/weblog/2003/09/07.html#a458.

  12. MBA says:

    Helpful For MBA Fans.

Comments are closed.

Skip to main content