Generics in an interface


R

Ray Costanzo

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() {}
}
}
 
Ad

Advertisements

R

Ray Costanzo

Never mind. I figured it out. I had to get rid of the constraint that T
needs new().

Thanks,

Ray at work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top