General parameters: providing various types

I have a class with two generic parameters. I want to restrict the second general parameter to a different type than the first. Is there any way to make this restriction at compile time? Type checking at runtime is not very useful.

public class Test<A, B> where B : not_typeof(A) { // ... } 
+4
source share
1 answer

The only way is at runtime.

I adapted the answer from the answer, which I posted in the comments.

 public class Test<A, B> { static Test() { if (typeof(B) == typeof(A)) { throw new NotSupportedException("Argument B is not supported."); } } } 
+4
source

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


All Articles