24

I was wondering if it is possible to add multiple generic constraints?

I have an Add method that takes an Object (Either Email, Phone or Address), so i was thinking something like:

public void Add<T>(T Obj) 
    where T : Address
    where T : Email
    where T : Phone
{
    if (Obj is Address)
        m_Address.Add(Obj as Address);
    else if (Obj is Email)
        m_Email.Add(Obj as Email);
    else
        m_Phone.Add(Obj as Phone);
}

But I keep getting:

"A constraint clause has already been specified for type parameter 'T'. All of the constraints for a type parameter must be specified in a single where clause."

7 Answers 7

28

You can't do that. Why not just have three methods and let the compiler do the hard work for you?

public void Add(Address address) { m_Address.Add(address); }
public void Add(Email email) { m_Email.Add(email); }
public void Add(Phone phone) { m_Phone.Add(phone); }
4
  • 1
    So, how to Enumerate over such a strongly typed generic collection of heterogeneous types like any other normal IEnumerable<T> ? Commented Nov 22, 2012 at 16:23
  • @AngshumanAgarwal - I'm assuming this is not an Add method on a collection as it wouldn't make a lot of sense.
    – Greg Beech
    Commented Nov 23, 2012 at 18:05
  • 1
    Suppose, if I have interface IWalk, IRun and IBothWalkRun. Now, I want to make a strongly typed heterogeneous collection out of all the types and iterate over such that it prints all who IWalk, IRun and IBothWalkRun ? Does that makes sense ? Commented Nov 23, 2012 at 20:31
  • @AngshumanAgarwal - That seems unrelated to this question. I think you'd be better off asking a new question than commenting here.
    – Greg Beech
    Commented Dec 4, 2012 at 9:30
9

CLR does not allow multiple inheritance, which is precisely what you're trying to express. You want T to be Address, Email an Phone at the same time (I assume those are class names). Thus is impossible. What's event more, this whole method makes no sense. You'll either have to introduce a base interface for all three classes or use three overloads of an Add method.

1
  • 11
    @Anton, I think the OP is asking about OR constraints (not AND). Commented Jul 24, 2009 at 10:56
5

How about creating an interface or base class for those three types?

But looking at your code, seems that you're not using generic well enough. The point of using generic is that you don't need to cast it into any particular type (in this case, you are).

3

You don't get any real benefits from generics in this case. I would just create different Add methods for each parameter Type.

3

Like others have said, in your specific case you should use inheritance or method overloading instead of generics. However, if you do need to create a generic method with multiple constraints then you can do it like this.

public void Foo<T>() where T : Bar, IBaz, new()
{
    // Your code here
}
1

In that case, I wouldn't bother, as you are comparing types anyway. Use this:

public void Add(object Obj)
{
    if (Obj is Address)
        m_Address.Add(Obj as Address);
    else if (Obj is Email)
        m_Email.Add(Obj as Email);
    else if (Obj is Phone)
        m_Phone.Add(Obj as Phone);
    else
        return;
}

I don't think multiple clauses are supported. You could also have separate method overloads too.

-4

where T : C1, C2, C3. Comma separated for constraints. Try using Base class or interfaces.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.