views:

85

answers:

1

(I would check this out for myself, but I don't have VS2010 (yet))

Say I have 2 base interfaces:

IBaseModelInterface
IBaseViewInterface

And 2 interfaces realizing those:

ISubModelInterface : IBaseModelInterface
ISubViewInterface : IBaseViewInterface

If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface>.

In C# 3 I can't do this even though the sub interfaces realize the base interfaces. And I'm pretty sure C# 4 lets me do this if I was using IEnumerable<IBaseModelInterface> because it's now defined with the in keyword to allow covariance. So does Tuple allow me to do this?

From what (little) I understand, covariance is only allowed on interfaces, so does that mean there needs to be an ITuple<T1, T2> interface? Does this exist?

+3  A: 

Tuple is a class (well, a family of classes) - it's invariant by definition. As you meantion later on, only interfaces and delegate types support generic variance in .NET 4.

There's no ITuple interface that I'm aware of. There could be one which would be covariant, as the tuples are immutable so you only get values "out" of the API.

Jon Skeet
I guess I could define my own IMyTuple<in TBaseModel, in TBaseView> that'd be neat
RichK
@RichK - if you do intend to write your own tuple class, you should carefully examine the comparison and equality semantics offered by BCL tuples. I assume you would want to preserve the same behavior as offered in the built in classes - and some of that behavior is non-obvious.
LBushkin