[Mono-dev] strange behaviour with generics

jrprodrigues at petrobras.com.br jrprodrigues at petrobras.com.br
Tue Nov 20 16:33:11 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?

Thanks, 

José Roberto P. Rodrigues
PETROBRAS/CENPES/Tecnologia de Reservatórios
tel.: 55-21-3865-6494 (rota: 812) - chave: bg11
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071120/df7e8b0d/attachment.html 


More information about the Mono-devel-list mailing list