In and Out Generic Modifier
Hi
Given the following 2 code examples:-
// Covariant interface.
interface ICovariant<out R> { }
// Implementing covariant interface.
class Sample<R> : ICovariant<R> { }
class Program
{
static void Test()
{
ICovariant<Object> iobj = new Sample<Object>();
ICovariant<String> istr = new Sample<String>();
// You can assign istr to iobj because
// the ICovariant interface is covariant.
iobj = istr;
}
}
/ Contravariant interface.
interface IContravariant<in A> { }
// Implementing contravariant interface.
class Sample<A> : IContravariant<A> { }
class Program
{
static void Test()
{
IContravariant<Object> iobj = new Sample<Object>();
IContravariant<String> istr = new Sample<String>();
// You can assign iobj to istr because
// the IContravariant interface is contravariant.
istr = iobj;
}
}
I thought Contravariance was Narrower (string) to Wider (object) and Covariance was Wider (object) to Narrower (string).
With the assignments above it indicates the vice versa?
Regards
Steven