public void ContravarianceTest()
        {
            IContravariance <BaseClass> b = new Contravariance();

            // IContravariance<BaseClass> => IContravariance<DerivedClass>로 implicit conversion.
            // 반대로는 불가능.
            IContravariance <DerivedClass> d = b;
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Vehicle v1 = new Car();
            //Vehicle v2 = new Car();
            //Vehicle v3 = new Aircraft();
            Func <Vehicle> func = GetVehicle; //#1
            Vehicle        v4   = func();     // For Func, input param is covariant and output param is conbtra.

            v4.getVehicle();                  //Only the Vehicle class properties and methods will be accessiable. Not of Car class.It will peel of anything related to Cars while assigning the object to Vehicle.
            Action <Car> action = ParkVehicle;

            action(new Car());                                             //#2 This is called contravariance, because we assigning more derived class to less derived class.
            //Action type parameters are IN, means all are contravariant.
            ICovariant <Vehicle>  covariant      = (ICovariant <Car>)null; //#1 Covariant - Assigning more derived class to less derived class.
            IContravariance <Car> contravariance = (IContravariance <Vehicle>)null;
        }
Ejemplo n.º 3
0
        public void Method()
        {
            ICovariance <DerivedClass> deriveList1 = null;
            ICovariance <BaseClass>    baseList1   = deriveList1;

            IContravariance <BaseClass>    baseList2   = null;
            IContravariance <DerivedClass> deriveList2 = baseList2;


            Func <object, FileStream> f2 = null;
            Func <string, Stream>     f1 = null;

            f1 += Ment1;
            f1 += Ment2;

            f1 = f2;
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            // Covariance:
            // ----------
            // Derived type assigned to type
            ICovariance <TitledMessage> a = new Covariance();
            ICovariance <Message>       b = a;
            Message v = b.Create("Hello world");

            Console.WriteLine(v.Content);

            // Contravariance:
            // -----------
            // Less derived type assigned to derived type
            IContravariance <Message>       d = new Contravariance();
            IContravariance <TitledMessage> f = d;

            f.Do(new TitledMessage {
                Content = "Hello world"
            });
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            List <int> list = new List <int>();

            list.AddRange(new int[] { 1, 2, 3, 4, 5 });
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

            MyGeneric <string> myGeneric = new MyGeneric <string>();

            myGeneric.SomeProperty = "Hello";
            Console.WriteLine(myGeneric.SomeMethod <int>(5));

            #region Covariance&Contravariance
            ICovariance <Child> covariance = new Covariance();
            Child child = covariance.DoSmth();
            child.DoSmth();
            ICovariance <Parent> covariance2 = covariance;
            Parent parent = covariance2.DoSmth();
            parent.DoSmth();

            child  = new Child();
            parent = new Parent();

            IContravariance <Parent> contravariance = new Contravariance <Parent>();
            contravariance.DoSmth(parent);
            IContravariance <Child> contravariance2 = contravariance;
            contravariance2.DoSmth(child);
            #endregion

            #region Arrays&Tuples
            (int, int)tuple = (1, 4);
            Console.WriteLine($"{tuple.Item1} {tuple.Item2}");
            var(name, age) = ("Tom", 23);

            int[][] jagged = new int[2][];
            jagged[0] = new int[3] {
                1, 2, 3
            };
            jagged[1] = new int[2] {
                4, 5
            };
            foreach (var line in jagged)
            {
                foreach (var item in line)
                {
                    Console.Write($"{item} ");
                }
                Console.WriteLine();
            }
            #endregion

            #region Enumerable
            foreach (var item in new MyEnumerable(7))
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();
            foreach (var item in new MyEnumerableYield(7))
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();
            foreach (var item in new MyEnumerableYield(15))
            {
                Console.Write($"{item} ");
            }
            Console.WriteLine();
            #endregion
        }