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