tags:

views:

66

answers:

3

can i :

    class X<T>
        where T : EventArgs
    {

    }

    class X<T>
        where T:Exception
    {

    }

in C#?

A: 

No, because the two clases do have the same name.

What are you trying to do?

Michael Stoll
+1  A: 

You can't use the same class name for the generic constraints.

You can use base classes as constraints, but the constraint then means that you can only use derived classes - in your examples, only classes that derive from EventArgs in the first example and classes that derive from Excpetion in the second.

See the documentation for constraints on type parameters.

Oded
exactly what i want, one class for T's that derive from one class and anothore for those deriving from another
Hellfrost
A: 

If you mean 2 classes with the same name in the same namespace with different generic type parameters - then NO, you can't!

You can only have one class in the where clause. You could use the

System.Runtime.InteropServices._Exception

interface to accept Exceptions (that would be an ugly hack, though).

If you put them in separate namespaces, you can.

Jaroslav Jandek