Can anyone fill in the gaps in my understanding here and explain why I get
this compiler error? Can I not use an interface as a generic type in an
interface? Below are contents to reproduce the problem. It seems that I'd
have to declare an empty constructor in the interface, but that cannot be
done in C#, correct? I'm using .net 2.0.
Error:
Error 1 The type 'SampleCode.ISampleInterface' must have a public
parameterless constructor in order to use it as parameter 'T' in the generic
type or method 'SampleCode.SomeClass<T>' IProblemInterface.cs
-------------------
Files:
IProblemInterface.cs:
using System;
namespace SampleCode
{
interface IProblemInterface
{
SomeClass<ISampleInterface> something { get; }
}
}
-------------------
ISampleInterface.cs:
using System;
namespace SampleCode
{
interface ISampleInterface
{
int i { get; }
}
}
-------------------
SomeClass.cs:
using System;
using System.Collections.Generic;
namespace SampleCode
{
class SomeClass<T> where T : ISampleInterface, new()
{
public SomeClass() {}
}
}
-------------------
Thank you,
Ray at work
Ray Costanzo - 12 Oct 2007 15:01 GMT
Never mind. I figured it out. I had to get rid of the constraint that T
needs new().
Thanks,
Ray at work
> Can anyone fill in the gaps in my understanding here and explain why I get
> this compiler error? Can I not use an interface as a generic type in an
[quoted text clipped - 47 lines]
>
> Ray at work