tags:

views:

53

answers:

4

I can restrict generics to a specify type using the "Where" clause such as:

public void foo<TTypeA>() where TTypeA : class, A

How do I do this if my function has two generic types?

public void foo<TTypeA, TTypeB>() where TTypeA : class, A && TTypeB : class, B

The above doesn't work. What's the correct syntax to add the rule "TTypeB : class, B"

+9  A: 
 public void foo<TTypeA, TTypeB>() where TTypeA : class, A 
                                   where TTypeB : class, B 
James Curran
More information about constraints on generic type parameters is available on MSDN, where they describe some of the other kinds of constraints possible (like naked constraints: `where TTypeB : TTypeA`). http://msdn.microsoft.com/en-us/library/d5x73970.aspx
LBushkin
Thanks everybody! It works. I'll mark this as the answer once SO lets me (apparently I have to wait 9 minutes)
Justin
+4  A: 
public void foo<TTypeA, TTypeB>() where TTypeA : class, A where TTypeB : class, B

dang, 20s late. Vote for James Curran, he was first.

jgauffin
+1  A: 

Something like this?

 public void foo<TTypeA, TTypeB>() where TTypeA : class where TTypeB : class
Flesrouy
A: 

just replace "&&" with another "where"

Zilog8