none
Generic type determined by constructor RRS feed

  • Question

  • I am faced with a problem that I would like to try to garner some opinions on. I have a class that I am trying to create that would look somewhat like:

    public class A<T>
    {
      private T[] _m;
      public A(T[] m)
      {
         _m =(T[])m.Clone();
      }
    
      public T[] M
      {
         get
         {
           return _m;
         }
      }
    . . .
    }
      

    Ideally I want to constrain 'T' to double or my implementation of 'Complex' but that would be another discussion. My question is how do I (or is it possible) get the type 'T' to be inferred from the constructor? I would like to be able to do something like the following:

    double[] x = new double[...];
    . . . 
    A<T> a = new A(x);
    

    Where 'T' takes on the type 'double' since that is what is passed in the constructor. I guess I could get the same effect if internally the array 'm' was an array of 'object. I could also use the new .NET 4.0 'dynamic' type. But I would rather keep this library in the 3.5 relm if possible. What solutions do you see? And why?

    Thank you.

    Kevin

     

     

    Monday, May 24, 2010 1:25 PM

Answers

  • You can't infer T from the constructor usage, but you can create a factory method that does your inference for you.

        public static class AFactory
        {
            public static A<T> Create<T>(T[] contents)
            {
                return new A<T>(contents);
            }
        }

     

        double[] y = new double[] { 1, 2, 3 };
        var a = AFactory.Create(y);

     

    As far as constraining T, though, it sounds like you're asking whether you can constrain it to be either a double or your 'Complex' type. If that's correct, the answer is no. You could constrain it to one or the other but not both at once.

    Tuesday, May 25, 2010 7:42 PM

All replies

  • There is no shortcut for this regarding the constructor.  Just type the following (using var to avoid typing A<double> again.)

    var a = new A<double>(x);

     

     

     

    Tuesday, May 25, 2010 12:03 AM
  • You can't infer T from the constructor usage, but you can create a factory method that does your inference for you.

        public static class AFactory
        {
            public static A<T> Create<T>(T[] contents)
            {
                return new A<T>(contents);
            }
        }

     

        double[] y = new double[] { 1, 2, 3 };
        var a = AFactory.Create(y);

     

    As far as constraining T, though, it sounds like you're asking whether you can constrain it to be either a double or your 'Complex' type. If that's correct, the answer is no. You could constrain it to one or the other but not both at once.

    Tuesday, May 25, 2010 7:42 PM
  • Hi Kevin,

    I think that both BinaryCoder and Chris are right. The issue is that you are asking for a runtime feature (i.e., the constructor determines the type of the parameter at runtime), while the generic parameter T is replaced by the compiler (i.e. at compile time) with the actual type.

    I hope this was helpful.

    Munir

    Thursday, June 3, 2010 1:16 PM