473,700 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1225
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.co m

"AdamH" <Ad***@discussi ons.microsoft.c om> wrote in message
news:3E******** *************** ***********@mic rosoft.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.co m

"AdamH" <Ad***@discussi ons.microsoft.c om> wrote in message
news:3E******** *************** ***********@mic rosoft.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(ISomeI nterface1 list, string b)
void MyMethod(ISomeI nterface2 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***@discussi ons.microsoft.c om> wrote in message
news:3E******** *************** ***********@mic rosoft.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**@eggheadca fe.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Robbe Morris [C# MVP] <in**@eggheadca fe.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.co m>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1666
by: Matthew W. Jackson | last post by:
I had a question about the "using" statement and Generics in the next version of C#, and I was directed to this newsgroup. My question is: Will the following syntax be valid? using Int32ArrayList = System.GCollections.ArrayList<Int32>; .... Int32ArrayList myArrayList = new Int32ArrayList();
11
3993
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we can expect when Generics are released? Thanks,
3
1953
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class Node<T> where T:IComparable<T> I don't like the syntax, and would prefer something that groups the constraint along with the type that it governs them, as depicted in this suggestion:
8
3363
by: Chris Dunaway | last post by:
The next version of VS.Net (Whidbey) promises to add generics support to VB. I have a vague remembrance of Templates in C++, but I guess I need a refresher. What will generics allow us to do? How do they make coding easier? Is there a resource that will give me a basic understanding of what generics are and how generics will be useful (in the context of VB.Net)? Thanks
1
1891
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList = new List<IPerson>(); ... // get the persons return personList;
9
1683
by: James Crosswell | last post by:
I'm not sure if I'm going about this the right way - it may be that Generics might be able to help me out here... but here goes: I have three classes as follows class BaseEdit class WidgetEdit: BaseEdit class FooEdit: BaseEdit These are all acutally Windows Forms and BaseEdit is an abstract class (and so never gets instantiated). I'd like to define a method in
13
3824
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
7
1288
by: Andy Bell | last post by:
Can this be done via .net generics? How? The % signs below are just to show how I want to do it, I realise they're not valid syntax. public abstract class BaseSelectionRequirement { ... protected Type mControlType; protected string mFieldName; protected Type mFieldType; protected UserControl mControl; ...
4
2070
by: wizard of oz | last post by:
I'm trying to write a data class using generics. This class is a Sparse Matrix meaning that it can have many dimensions but not many entries (e.g. a 100 by 100 matrix, but there might only be 10 entries in the matrix). I intend to use LinkedLists or Trees to manage the row and column keys and probably a hash Map to manage the cells - but that's not important right now. My first step is that in my add method, I need to ensure that my...
0
913
by: wpmccormick | last post by:
I'm trying to make a custom Comparer for a priority queue and I am stumped on the syntax for the Generics. I think I almost have it (below) except for where the ??'s are. Anybody done this before? Thanks, Bill struct Item<TValue, TPriority{ }
0
8722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8643
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9069
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8968
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4401
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2025
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.