[Mono-dev] strange behaviour with generics
Marek Safar
marek.safar at seznam.cz
Wed Nov 21 07:22:16 EST 2007
Hello,
Please fill a bug report at http://www.mono-project.com/Bugs with your
separate class.
Thanks
Marek
> Hi,
>
> I am using version 1.2.5.1 <http://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
> ------------------------------------------------------------------------
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
More information about the Mono-devel-list
mailing list