Freigeben über


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

  • Anonymous
    August 05, 2003
    That's really cool! How long before us common folks could use it?
  • Anonymous
    August 05, 2003
    So the 'ECMA process' can push back the release date of Whidbey, while its mainly ready for releasing ? :D
  • Anonymous
    August 06, 2003
    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?
  • Anonymous
    August 08, 2003
    Good stuff :) Can't wait to get my hands on generics. I wonder how performance is on generics...
  • Anonymous
    August 09, 2003
    Could you please comment more on the 'typecasting' with generics, i.e., in your code: list.Add ((T)reader[0]);
  • Anonymous
    August 10, 2003
    Scary. Remember ATL and people running away for Java. Obviously the same mistakes are going to be made again.
  • Anonymous
    August 11, 2003
    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 :)
  • Anonymous
    August 12, 2003
    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?
  • Anonymous
    August 17, 2003
    The comment has been removed
  • Anonymous
    August 25, 2003
    "generics" is that like STL for C++? Sure looks like it! Then that's way cool, can't wait for it!
  • Anonymous
    September 07, 2003
    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.
  • Anonymous
    December 18, 2004
    Helpful For MBA Fans.