예제 #1
0
        static void Main(string[] args)
        {
            //
            // -------------------- 20.+21. Example 4 - Operator Overloading & Indexer  --------------------
            //
            MyList <int> list1 = new() { 2, 3, 5, 8 };
            MyList <int> list2 = new() { 1, 1, 1, 1 };
            MyList <int> list3 = list1 + list2;

            foreach (int ele in list3)
            {
                Console.WriteLine(ele);
            }


            //
            // -------------------- 19. Example 3 - Generic Class  --------------------
            //

            /*
             * MyList<int> listOne = new();
             * listOne.Add(12);
             * listOne.Add(3);
             * listOne.Add(42);
             * foreach (int ele in listOne)
             * {
             *  Console.WriteLine(ele);
             * }
             *
             * for (int i = 0; i < listOne.Count; i++)
             * {
             *  var backwardsCount = listOne.Count - 1 - i;
             *  var item = listOne[listOne.Count - 1 - i];
             *  Console.WriteLine(item);
             * }
             * //*/


            //
            // -------------------- 18. Example 3 - Object specific generics  --------------------
            //

            /*
             * Person p1 = new Person { Age = 27 };
             * Person p2 = new() { Age = 27 };
             * Console.WriteLine(AreEqual(p1, p2));
             * Console.WriteLine(p1.CompareTo(p2));
             * //*/


            //
            // -------------------- 17. Example 2 - Sorting collections --------------------
            //

            /*
             * int[] intArray = { 2, 5, 8, 1, 2, 42, 9, 46, 9, 12, 34, 62, 79, 45, 62, 0 };
             * string[] strArray = { "aabc", "abba", "baac", "babc", "abac" };
             *
             * var sortedArray = MySort(strArray);
             * System.Console.WriteLine(string.Join(", ", sortedArray));
             * //*/



            //
            // -------------------- 16. Example 1 - Simple method --------------------
            //

            /*
             * Console.WriteLine($"{AreEqual("a", "a")}");
             * Console.WriteLine($"{AreEqual(1, 1)}");
             * Console.WriteLine($"{AreEqual(true, true)}");
             * //*/
        }

        //
        // -------------------- 17. Example 2 - Sorting collections --------------------
        //
        public static T[] MySort <T>(T[] array) where T : IComparable <T>
예제 #2
0
        static void Main(string[] args)
        {
            //1
            MyList <int> myList1 = new MyList <int>();

            Console.WriteLine(myList1.Count);

            myList1.Add(7);
            myList1.Add(9);
            myList1.Add(0);
            myList1.Add(3);

            Show(myList1);

            MyList <int> myList2 = new MyList <int>(new int[] { 1, 3, 4, 9 });

            Show(myList2);

            myList2.AddRange(new int[] { 8, 8, 8, 8 });
            Show(myList2);

            Console.WriteLine();
            //2
            Dictionary <int, string> dict = new Dictionary <int, string>();

            MyDictionary <int, string> myDictionary = new MyDictionary <int, string>();

            myDictionary.Add(1, "книга");
            myDictionary.Add(3, "солнце");
            myDictionary.Add(4, "яблоко");
            myDictionary.Add(2, "ручка");
            myDictionary.Add(5, "стол");

            Console.WriteLine(myDictionary.Count);

            Console.WriteLine(myDictionary["книга"]);
            Console.WriteLine(myDictionary["дом"]);
            Console.WriteLine(myDictionary["ручка"]);
            Console.WriteLine(myDictionary["стол"]);
            Console.WriteLine(myDictionary["карандаш"]);
            Console.WriteLine(myDictionary["яблуко"]);
            Console.WriteLine(myDictionary["солнце"]);
            Console.WriteLine(myDictionary["sun"]);
            Console.WriteLine(myDictionary["table"]);

            Console.WriteLine();

            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(myDictionary[i]);
            }

            Console.WriteLine();
            //3
            Array.ForEach(myList2.GetArray(), Console.Write);

            Console.WriteLine();
            //4
            var aInt = MyClass <int> .FactoryMethod();

            aInt = 10;

            if (aInt.GetType() == typeof(int))
            {
                Console.WriteLine(aInt);
            }

            var bDouble = MyClass <double> .FactoryMethod();

            bDouble = 13.894;

            if (bDouble.GetType() == typeof(double))
            {
                Console.WriteLine(bDouble);
            }
        }