Generics Syntax help

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 )
 
N

Nicholas Paldino [.NET/C# MVP]

AdamH,

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

Hope this helps.
 
G

Guest

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 )
 
M

Marc Gravell

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
 
R

Robbe Morris [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

c# generic and methods 3
generics problem 2
Generics 2
Polymorphic generics 4
Help with generics 2
Question regarding generics and type constraints 6
Generics in C# 1
Generics Questions 10

Top