Generic methods help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fabrizio Romano

    Generic methods help

    Hello,

    I have a problem with a generic method.
    I have written a sieve to generate prime numbers. This method takes an input
    parameter which is the upperbound of the last prime I need to get. So if I
    call
    List<intp = Sieve(10);
    I get a List<intwith 2,3,5,7 inside.
    Sometimes I need a list of long values so I have another method, called
    SieveLong(int upTo) which returns a List<long>. It does the same exact
    operations of the int version, but returns a generic list of long values.
    Now, I would like to concentrate these 2 methods into one single one, so I
    wrote something like:

    public List<TSieve<T>( int upTo) where T : struct {}

    to be able to get a List<intor a List<longdepend ing on how I call the
    method Sieve<intor Sieve<longin my code.
    The problem is that when it comes to put into the list the first prime
    number (2) I get an error.

    the code is something like this:
    ....
    List<Tp=new List<T>();
    p.Add((T)2);
    ....

    It tells me that it can't convert int to T. I tried adding different
    constraints, like IComparable, IConvertible, and so on, but I can't get this
    to work.

    Basically I need that depending on how I call the Sieve method, it returns
    me a certain type list, like this:

    List<intp = Sieve<int>(upTo );
    or
    List<longp=Siev e<long>(upTo);

    and I would like this to be possible with Int16, Int32, Int64 types.
    Any help?

    Thank you very much,
    Fabrizio


  • Marc Gravell

    #2
    Re: Generic methods help

    If you are using .NET 3.5 then yes!
    This very morning Jon released some of my code in the MiscUtil library
    (link below) that includes generic operator support, and an equally
    handy Convert mechanism.

    Operators: http://www.pobox.com/~skeet/csharp/g...operators.html
    Download: http://www.pobox.com/~skeet/csharp/miscutil/

    For instance, you can use:

    p.Add(Operator. Convert<int,T>( 2));

    You might also (instead) be able to cast to object in the middle, but
    the above avoids a box. Additionally, I suspect that you might find
    the other Operator.{blah} methods (divide, add, multiply, etc) handy;
    note that it doesn't include a Modulo function at the moment (which
    would be handy here), but I could add one if needed?

    However!!!! Since there are only 3 types here, you might also consider
    simply having SieveInt32, SieveInt64; but of course, if this code is
    at the bottom of a pile of generics, then the Operator.{blah} approach
    may be more useful (since you can use Sieve<Tfor whichever T you
    happen to have...)

    Marc


    Comment

    • Fabrizio Romano

      #3
      Re: Generic methods help

      All right, thank you, I'll give it a look.

      Fabrizio

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:%23ZpEoPLb IHA.4344@TK2MSF TNGP02.phx.gbl. ..
      If you are using .NET 3.5 then yes!
      This very morning Jon released some of my code in the MiscUtil library
      (link below) that includes generic operator support, and an equally handy
      Convert mechanism.
      >
      Operators: http://www.pobox.com/~skeet/csharp/g...operators.html
      Download: http://www.pobox.com/~skeet/csharp/miscutil/
      >
      For instance, you can use:
      >
      p.Add(Operator. Convert<int,T>( 2));
      >
      You might also (instead) be able to cast to object in the middle, but the
      above avoids a box. Additionally, I suspect that you might find the other
      Operator.{blah} methods (divide, add, multiply, etc) handy; note that it
      doesn't include a Modulo function at the moment (which would be handy
      here), but I could add one if needed?
      >
      However!!!! Since there are only 3 types here, you might also consider
      simply having SieveInt32, SieveInt64; but of course, if this code is at
      the bottom of a pile of generics, then the Operator.{blah} approach may be
      more useful (since you can use Sieve<Tfor whichever T you happen to
      have...)
      >
      Marc
      >

      Comment

      Working...