Set where for the general type

I need to indicate that the generic type for my class implements the interface as well as the reference type. I tried both code snippets below but didn't work

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

I cannot specify several where clauses for a type, is it possible to do this?

+4
source share
2 answers

The last syntax must be accurate (and compiles for me). The first does not work because you are trying to provide two constraints on the same type parameter, and not on different type parameters.

Please give a short but complete example of the latest syntax that does not work for you. This works for me:

 public interface IFoo {} public abstract class MyClass<T> where T : class, IFoo { } 
+7
source

The question is how to define multiple links where links are here as a duplicate. If this question is indeed a duplicate, than the full answer should contain both cases.

Case 1 - A single pedigree has several limitations:

 public interface IFoo {} public abstract class MyClass<T> where T : class, IFoo { } 

Case 2 - Several generics, each with its own limitations:

 public interface IFoo1 {} public interface IFoo2 {} public abstract class MyClass<T1, T2> where T1 : class, IFoo1 where T2 : IFoo2 { } 
+5
source

Source: https://habr.com/ru/post/1286061/


All Articles