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();
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
    public static void Main()
    {
        ICovariant <string>     a_2 = null;
        IContravariant <object> b_2 = null;

        Test(a_2, b_2);
    }
Esempio n. 4
0
    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;
        }
Esempio n. 6
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);
        }

        return(0);
    }
Esempio n. 7
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);
    }
Esempio n. 8
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)
 {
 }
Esempio n. 10
0
 public static void SomeMethod(IContravariant <ContravariantDerivedClass> tmp)
 {
     //.....
 }
Esempio n. 11
0
 public static bool Contra <T> (IContravariant <T> e1, IContravariant <T> e2)
 {
     Console.WriteLine(typeof(T));
     return(typeof(T) == typeof(string));
 }
Esempio n. 12
0
 public void DoSomething(IContravariant <IvidataLinkDerived> ividataLinkDerived)
 {
     ividataLinkDerived.SetIvidataLinkObject(new IvidataLinkDerived());
     ividataLinkDerived.Input = new IvidataLinkDerived();
 }
Esempio n. 13
0
 public void Contravariant(IContravariant <Apple> person)
 {
 }
Esempio n. 14
0
 private void Contravariant(IContravariant <Apple> apple)
 {
 }