private void ContravarianceExample() { ConsoleExtensions .WriteColoredLine(Resources.ContravarianceExampleRunning, ConsoleColor.Cyan); // A contravariant type in an interface or delegate can be converted to a more SPECIFIC // type. // Interface: IContravariant<Fruit> to IContravariant<Apple> IContravariant <Apple> appleContravariantInterface = this.fruitContravariantInterface; // Delegate: Contravariant<Fruit> to Contravariant<Orange> Contravariant <Orange> orangeContravariantDelegate = this.fruitContravariantDelegate; const int AppleQuality = 75; const int OrangeQuality = 60; var appleValue = appleContravariantInterface.GetValue( new Apple(AppleColor.Red, AppleQuality)); var orangeValue = orangeContravariantDelegate(new Orange(OrangeQuality)); Console.WriteLine( Resources.ContravarianceExampleAppleValue, nameof(appleValue), appleValue); Console.WriteLine( Resources.ContravarianceExampleOrangeValue, nameof(orangeValue), orangeValue); Console.WriteLine(); }
static void Main(string[] args) { // Enables you to use a more derived type than originally specified. ICovariant <string> co = null; ICovariant <object> moreGeneralType = co; // Enables you to use a more generic (less derived) type than originally specified. IContravariant <object> contra = null; IContravariant <string> moreGenericType = contra; // ICovariant can be declared with the base or every class that base inherit from CovariantDerivedClass coDerived = new CovariantDerivedClass(); ICovariant <CovariantBaseClass> coBase = coDerived; // ICovariant can be declared with the base or every class that base inherit from ContravariantBaseClass contraBase = new ContravariantBaseClass(); IContravariant <ContravariantDerivedClass> contraDerived = contraBase; // Work in both ways SomeMethod(contraBase); SomeMethod(contraDerived); }
public static void Main() { ICovariant <string> a_2 = null; IContravariant <object> b_2 = null; Test(a_2, b_2); }
public static int Main() { var b = new B(); var c = new C(); IBothVariants <A, C> both = new BothVariants <B, B> (b); if (both.Bar(c) != (b.GetHashCode() ^ c.GetHashCode())) { return(1); } IInvariant <B> neither = new Invariant <B> (); ICovariant <A> co = neither; if (co.Foo.Fruit != "Banana") { return(2); } IContravariant <C> contra = neither; if (contra.Bar(c) != c.GetHashCode()) { return(3); } return(0); }
/// <summary> /// Initializes a new instance of the <see cref="VarianceExample"/> class. /// </summary> /// <param name="orangeInvariant">An interface with an invariant type of /// <see cref="Orange"/>.</param> /// <param name="bananaCovariant">An interface with a covariant type of /// <see cref="Banana"/>.</param> /// <param name="fruitContravariant">An interface with a contravariant type of /// <see cref="Fruit"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="orangeInvariant"/>, /// <paramref name="bananaCovariant"/>, or <paramref name="fruitContravariant"/> is /// <see langword="null"/>.</exception> public VarianceExample( IInvariant <Orange> orangeInvariant, ICovariant <Banana> bananaCovariant, IContravariant <Fruit> fruitContravariant) { ParameterValidation.IsNotNull(orangeInvariant, nameof(orangeInvariant)); ParameterValidation.IsNotNull(bananaCovariant, nameof(bananaCovariant)); ParameterValidation.IsNotNull(fruitContravariant, nameof(fruitContravariant)); this.orangeInvariantInterface = orangeInvariant; this.bananaCovariantInterface = bananaCovariant; this.fruitContravariantInterface = fruitContravariant; }
public static int Main() { ICovariant <object> a = null; ICovariant <string> b = null; if (!Covariant(a, b)) { return(1); } IContravariant <string> a_1 = null; IContravariant <object> b_1 = null; if (!Contra(a_1, b_1)) { return(2); } return(0); }
public static int Main() { ICovariant <object> a = null; ICovariant <string> b = null; if (!Covariant(a, b)) { return(1); } IContravariant <string> a_1 = null; IContravariant <object> b_1 = null; if (!Contra(a_1, b_1)) { return(2); } ICovariant <string> a_2 = null; IContravariant <object> b_2 = null; IContravariant <string> c_2 = null; if (!CovContCont(a_2, b_2, c_2)) { return(3); } IContravariant <object> a_3 = null; ICovariant <string> b_3 = null; IContravariant <string> c_3 = null; ICovariant <string> d_3 = null; if (!ContCovContCov(a_3, b_3, c_3, d_3)) { return(4); } Console.WriteLine("ok"); return(0); }
public static void Main(string[] args) { // covariance { ICovariant <A> ia = null; ICovariant <IBase> ibase = null; // this assignment is legal because the type-parameter T in Contravariant<T> // is declated covariant ibase = ia; } // contravariance { IContravariant <A> ia = null; IContravariant <IBase> ibase = null; // this assignment is legal because the type-parameter T in Contravariant<T> // is declated contravariant ia = ibase; } Console.WriteLine("Hello World!"); }
public void Contravariant(IContravariant <Apple> apple) { }
public static void SomeMethod(IContravariant <ContravariantDerivedClass> tmp) { //..... }
public static bool Contra <T> (IContravariant <T> e1, IContravariant <T> e2) { Console.WriteLine(typeof(T)); return(typeof(T) == typeof(string)); }
public void DoSomething(IContravariant <IvidataLinkDerived> ividataLinkDerived) { ividataLinkDerived.SetIvidataLinkObject(new IvidataLinkDerived()); ividataLinkDerived.Input = new IvidataLinkDerived(); }
public void Contravariant(IContravariant <Apple> person) { }
private void Contravariant(IContravariant <Apple> apple) { }