Home > front end >  Generic method with multiple optional constraints but only one Generic Parameter (c#)
Generic method with multiple optional constraints but only one Generic Parameter (c#)

Time:09-17

Is something like this even possible. I want the case, that it executes, when T is MyClass or MyOtherClass.

public T Method<T>()
   where T: MyClass, T: MyOtherClass
   { //execution }

CodePudding user response:

This is not possible but you can use an interface for this.

For example:

public interface IMyInterface...

public class MyClass : IMyInterface...

public class MyOtherClass : IMyInterface...

public T Method<T>() where T : IMyInterface...
  • Related