I have the following situation:
public class SomeClass {/*… */}
public interface ISomeInterface {/*… */}
public T GetFirst<T>(){/*… gets the first object of type T */}
public void AddElement<T> () where T: SomeClass, ISomeInterface {/*… */}
What I would like to do is call GetFirst with the Type parameter being anything that derives from both SomeClass and ISomeInterface.
As an example, if I had the following classes:
class A : SomeClass, ISomeInterface { }
class B : SomeClass, ISomeInterface { }
class C : SomeClass, ISomeInterface { }
And I want to specify the type parameter of GetFirst() to return any of A, B, or C, so the result could satisfy the type constraint of AddElement:
void MyFunction()
{
t result = GetFirst<t>() where t : SomeClass, ISomeInterface;
AddElement(result);
}
Is it possible to define multiple type constraints when supplying a type parameter in C#?
AddElement- I'm not sure why you're not just applying that toGetFirst...GetFirstshouldn't be generic at all. Fundamentally I think the question is unclear at the moment - I suspect the OP has misunderstood generics, but without more information it's going to be hard to know exactly what the misunderstanding is, or how to help them.