Exemplo n.º 1
0
        static void Main()
        {
            GenericList <int> integerList = new GenericList <int>();

            Console.WriteLine(integerList.Capacity); // 16
            Console.WriteLine(integerList.Count);    // 0

            integerList.Add(1);
            integerList.Add(2);
            integerList.InsertAt(1, 3);
            Console.WriteLine(integerList.Capacity);   // 16
            Console.WriteLine(integerList.Count);      // 3
            Console.WriteLine(integerList);            // {1, 3, 2}
            Console.WriteLine(integerList.IndexOf(1)); // 0
            Console.WriteLine(integerList.Exists(0));  // False
            Console.WriteLine(integerList.Exists(2));  // True

            integerList.RemoveAt(0);
            Console.WriteLine(integerList);                         // {3, 2}
            Console.WriteLine(integerList.IndexOf(1));              // -1
            Console.WriteLine(GenericList <int> .Min(integerList)); // 2
            Console.WriteLine(GenericList <int> .Max(integerList)); // 3

            integerList.InsertAt(12, 7);
            Console.WriteLine(integerList.Count); // 13
            Console.WriteLine(integerList);       // {3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7}

            System.Reflection.MemberInfo info = typeof(GenericList <>);
            foreach (object attribute in info.GetCustomAttributes(false))
            {
                Console.WriteLine(attribute);
            }
        }
Exemplo n.º 2
0
        static void Main()
        {
            GenericList <int> nums = new GenericList <int>();

            nums.Add(5);
            nums.Add(10);
            nums.Add(50);

            Console.WriteLine(nums.Contains(10)); // output: True
            Console.WriteLine(nums[2]);           // output: 50

            nums[2] = 42;
            Console.WriteLine(nums[2]);           // output: 42

            Console.WriteLine(nums.IndexOf(42));  // output: 2

            Console.WriteLine(nums.Count);        // output: 3

            Console.WriteLine(nums);              // output: [item1, item2...]

            nums.RemoveAt(0);
            Console.WriteLine(nums.Count);        // output: 2

            nums.Clear();
            Console.WriteLine(nums.Count);        // output: 0
        }
Exemplo n.º 3
0
        public static void Main()
        {
            GenericList <int> genericList = new GenericList <int>()
            {
                1, 2, 3, 100
            };

            Print(genericList, "New GenericList");

            // Clear
            genericList.Clear();
            Print(genericList, "Clear");

            // Add
            genericList.Add(1);
            genericList.Add(3);
            genericList.Add(8);
            genericList.Add(69);
            Print(genericList, "Add // 1, 3, 8, 69");

            // Insert on specific index
            genericList.Insert(4, 5);
            Print(genericList, "Insert at index // 0 -> 5");

            // Remove by index
            genericList.RemoveAt(4); // will remove 69
            Print(genericList, "RemoveAt // index 4");

            // Add range
            genericList.AddRange(100, 200, 300, 400);
            Print(genericList, "Add range // 100, 200, 300, 400");

            // Add on specific index
            genericList[0] = 69;
            Print(genericList, "Insert at index // 0");

            // Print element that is on specific index
            Print(genericList[0], "Show by index // 0");

            // Get item's index, if item not presented returns -1
            Print(genericList.GetIndex(100), "Find item's index // 100");
            Print(genericList.GetIndex(1000), "Find item's index // 1000");

            // Contains
            Print(genericList.Contains(300), "Check if collaction contains // 300");
            Print(genericList.Contains(3000), "Check if collaction contains // 3000");

            // Show total capacity
            Print(genericList.Capacity, "Collection capacity");

            // Show size
            Print(genericList.Size, "Collection size");

            // Minimum, extension generic method
            genericList.Add(-5);
            Print(genericList.Minimum(), "Find min element");

            // Maximum, extension generic method
            Print(genericList.Maximum(), "Find max element");

            // Foreaching through elements
            Console.WriteLine("Foreaching through elements");
            foreach (var item in genericList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine(genericList.Average());

            // New generic list of DateTime
            IGenericList <DateTime> dates = new GenericList <DateTime>();

            dates.Add(DateTime.MaxValue);
            dates.Add(DateTime.Now);

            Print(dates, "Print dates");
        }
Exemplo n.º 4
0
        static void Main()
        {
            var list = new GenericList <int>(4);

            list.Add(22);
            list.Add(33);
            list.Add(1);
            list.Add(16);
            list.Add(8);
            list.Add(21);
            list.Add(3);
            list.Print();

            list.Insert(0, 9);
            list.Print();

            var index = list.IndexOf(33);

            Console.WriteLine(index);

            var tr = list.Contains(16);

            Console.WriteLine(tr);

            var fl = list.Contains(102);

            Console.WriteLine(fl);

            var find = list.FindIndex(x => x == 1);

            Console.WriteLine(find);

            list.Remove(33);
            list.Print();

            //exception
            //list.Remove(332);
            //list.Print();

            list.RemoveAt(1);
            list.Print();

            var min = list.Min();
            var max = list.Max();

            Console.WriteLine(min);
            Console.WriteLine(max);

            list.Clear();
            list.Print();

            //Problem 4. Generic List Version

            var typeOfList = typeof(GenericList <>);
            var atributes  = typeOfList.GetCustomAttributes(false);

            foreach (VersionAttribute attribute in atributes)
            {
                Console.WriteLine("Version " + attribute.Version);
            }
        }