Excuse me if this is a dupe, but I couldn't seem to get the right combo of keywords to filter down the various type constraint and generics questions out there (as there are a lot).
I have two interfaces--let's call them IOnline and IOffline.
They're closely related in that they describe nearly identical contracts, but one of the key differences between them is the context in which the concrete implementations will be used. It's not exactly my circumstances, but it illustrates the problem well.
I then have some methods out there that do work against the concrete implementers of these interfaces. Sometimes these methods only want to deal with one type and not the other.
Simple enough:
public void DoStuff<T>(string foo) where T : IOnline {}
The kicker is implementing the code for methods that can operate on EITHER type. I thought this would be correct, but in reading the compilation error, my expectation that the constraint would be interpreted as "allow any type T to be used generically here if they implement IOnline OR IOffline", is actually being interpreted as "Allow any type T to be used generically here if they implement BOTH".
public void DoStuff<T>(string foo) where T : IOnline, IOffline {}
Trying to implement two separate methods with the same name but different constraints fails as there's obvious ambiguity issues--we're not overloading since the parameter list is the same (since the desired behavior is identical).
I could use two different names for two different methods, each with the appropriate constraint, but that seems kludgy and makes other things downstream to be a pain in the ass...doable, but not ideal.
I feel like there must be something I'm missing here... I feel perfectly comfortable in generic land but this is the first time I've ever had to accomplish what I'm after and I feel like I'm just spinning my wheels atm.