471,336 Members | 1,254 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Generics Syntax help

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 )

Jan 19 '06 #1
6 1167
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]
- mv*@spam.guard.caspershouse.com

"AdamH" <Ad***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
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 )


Jan 19 '06 #2
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"AdamH" <Ad***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
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 )



Jan 19 '06 #3
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
Jan 19 '06 #4
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 - 2004-2006 Microsoft MVP C#
http://www.eggheadcafe.com/forums/merit.asp

"AdamH" <Ad***@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
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 )


Feb 26 '06 #5
Robbe Morris [C# MVP] <in**@eggheadcafe.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 26 '06 #6
Yep, I guess I couldn't see a design reason to do
this either. Some sort of factory pattern "may be"
the route to go or some scheme of method overloads
perhaps.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
http://www.eggheadcafe.com/forums/merit.asp

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Robbe Morris [C# MVP] <in**@eggheadcafe.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Matthew W. Jackson | last post: by
11 posts views Thread by andrew queisser | last post: by
3 posts views Thread by Marshal | last post: by
8 posts views Thread by Chris Dunaway | last post: by
1 post views Thread by Peter Kirk | last post: by
9 posts views Thread by James Crosswell | last post: by
13 posts views Thread by rkausch | last post: by
7 posts views Thread by Andy Bell | last post: by
4 posts views Thread by wizard of oz | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.