tags:

views:

69

answers:

2

I need to specify that a generic type for my class implements an interface, and is also a reference type. I tried both the code snippets below but neither work

public abstract class MyClass<TMyType> 
   where TMyType : IMyInterface
   where TMyType : class

public abstract class MyClass<TMyType> 
       where TMyType : class, IMyInterface

I'm unable to specify multiple where clauses for a type, is it possible to do this?

+2  A: 

The latter syntax should be fine (and compiles for me). The first doesn't work because you're trying to provide two constraints on the same type parameter, not on different type parameters.

Please give a short but complete example of the latter syntax not working for you. This works for me:

public interface IFoo {}

public abstract class MyClass<T>
    where T : class, IFoo
{
}
Jon Skeet
Ok, thanks. You're right, the second snippet does work. Sorry - compile error I got was something unrelated to this when I tried. Should have read it properly!!
Charlie
A: 

The second syntax is the correct one.
Which error are you getting?

Paolo Tedesco