Generics Syntax help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Anyone know if you can have an OR type of contraint in the where clause?

Psudo example
Void MyMethod<T>(T list, string b) where T:interface1 OR interface2 {}

Does that make sense? ( T should implement at least one of the interfaces (
interface1 and/or interface2 )
 
AdamH,

No such constraint exists. The constraints are all and operations,
meaning each one has to apply.

Hope this helps.
 
Suspected that to be the truth, guess I was hoping otherwise ( or at least
for a cheat )
..
I’ve been wrong so many times before……..
Thx just the same:)


Nicholas Paldino said:
AdamH,

No such constraint exists. The constraints are all and operations,
meaning each one has to apply.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AdamH said:
Anyone know if you can have an OR type of contraint in the where clause?

Psudo example
Void MyMethod<T>(T list, string b) where T:interface1 OR interface2 {}

Does that make sense? ( T should implement at least one of the interfaces
(
interface1 and/or interface2 )
 
Well, from your example it isn't 100% clear what you are doing with T, but
is it possible you could do this with straight overloading?

i.e.

void MyMethod(ISomeInterface1 list, string b)
void MyMethod(ISomeInterface2 list, string b)

Which could then perhaps call down to some private function that will use
either?

Marc
 
I can't find any mention of this functionality in the docs
as I read them. You can specify more than one
interface contract per generic type but the docs
appear to infer that both constraints would be
required and not in the form of an OR clause.
 
Robbe Morris said:
I can't find any mention of this functionality in the docs
as I read them. You can specify more than one
interface contract per generic type but the docs
appear to infer that both constraints would be
required and not in the form of an OR clause.

Indeed - and even if it *were* available, I can't see how it would be
useful. You'd have to keep checking which interface was implemented in
order to use it.
 
Back
Top