[Mono-dev] strange behaviour with generics

Felipe Portella felipeportella.listas at gmail.com
Tue Nov 20 20:15:34 EST 2007


Hi,

I am using version 1.2.5.1 of Mono and I am having some troubles in using a
generic parameter in a method, which seems to be a bug .

The following code compiles fine:


using  System;

namespace  TestMethodParameterTypeMono
{
    class   Program
   {
      static   void  Main( string [] args)
      {
          Class1 < double > myClass1 = new   Class1 < double >();
          int  i = myClass1.MethodB< DoubleCalculator >();
      }
   }

    public   interface   ICalculator <T>
   {
      T Add(T a, T b);
   }

    public   struct   DoubleCalculator  : ICalculator < double >
   {
      public   double  Add( double  a, double  b)
      {
          return  a + b;
      }
   }

    public   class   Class1 <T>
      where  T : IComparable <T>
   {
      private   int  i = 5;
      public   int  MethodB<C>() where  C : ICalculator <T>, new ()
      {
          return  i;
      }
   }
}


On the other hand, if I take the class Program and put it in a different
file and compile it isolated making a reference to the previous generated
.exe, the following compilation error appears:

Program.cs(13,27): error CS0309: The type
`TestMethodParameterTypeMono.DoubleCalculator' must be convertible to
`TestMethodParameterTypeMono.ICalculator<
TestMethodParameterTypeMono.DoubleCalculator>' in order to use it as
parameter `C' in the generic type or method
`TestMethodParameterTypeMono.Class1<double>.MethodB<C>()'



Note that the constraint on type parameter C on MethodB is to implement
ICalculator<double>, which DoubleCalculator does, instead of
ICalculator<DoubleCalculator> as the message suggests.

Am I missing something or is it really a bug? The separate class Program
compiles fine in Visual Studio 2005 making the reference.

Thanks,
Felipe Portella
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071120/05817e63/attachment.html 


More information about the Mono-devel-list mailing list