Exemple #1
0
        static void Main()
        {
            // Methods Add, Count, Clear, Contains, and indexer

            Console.WriteLine("Enter list size");
            string str = Console.ReadLine();
            int    u   = string.IsNullOrEmpty(str) ? 0 : Convert.ToInt32(str);

            var list = new MyList <int>();

            var t = new Random();

            for (int x = 0; x < u; x++)
            {
                list.Add(t.Next(100));
            }

            Console.WriteLine("Elements of list are");
            for (int i = 0; i < list.Count; i++)
            {
                Console.Write(list[i] + " ");
            }
            Console.WriteLine();
            Console.WriteLine(new string('-', 30));

            //foreach (var item in list)  //is not working , no getenumerator
            //{

            //}

            Console.WriteLine("Enter searching number");
            if (list.Contains(Convert.ToInt32(Console.ReadLine())))
            {
                Console.WriteLine("List contains this number");
            }
            else
            {
                Console.WriteLine("no matchies");
            }


            var arr = list.GetArray();

            foreach (var item in arr) // working because GetArray returns Array tape
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            var myList1 = new MyList <int>();

            OutputMyList(myList1);

            var myList2 = new MyList <int>(10);

            OutputMyList(myList2);

            var myList3 = new MyList <int>(10, 9);

            OutputMyList(myList3);

            myList1.Add(10);
            OutputMyList(myList1);

            myList2.AddRange(new int[] { 20, 20, 20 });
            OutputMyList(myList2);

            myList1.Clear();
            OutputMyList(myList1);

            Console.WriteLine(myList3.Contains(9));
            OutputMyList(myList3);

            var array = Tools.CreateUnsortedArray(20);

            Sorts.Sorts.MergeSort(array);
            myList1.AddRange(array);
            var aim = (new Random()).Next(20);

            Console.WriteLine($"Aim: {aim}");
            OutputMyList(myList1);
            Console.WriteLine(myList1.BinarySearch(aim));

            OutputMyList(myList1.ConvertAll(value => (char)value));

            array = new int[20];
            myList1.CopyTo(array);
            myList1.AddRange(array);
            OutputMyList(myList1);

            Console.WriteLine("Exists: " + myList1.Exists(x => x == 10));
            Console.WriteLine("Find: " + myList1.Find(x => x == 10));
            Console.WriteLine("FindLast: " + myList1.FindLast(x => x == 10));
            Console.WriteLine("FindAll.Count: " + myList1.FindAll(x => x == 10)?.Count);
            Console.WriteLine("FindIndex: " + myList1.FindIndex(x => x == 10));
            Console.WriteLine("FindLastIndex: " + myList1.FindLastIndex(x => x == 10));
            Console.WriteLine("IndexOf: " + myList1.IndexOf(10));
            Console.WriteLine("LastIndexOf: " + myList1.LastIndexOf(10));

            OutputMyList(myList1.GetRange(3, 4));
            myList1.Insert(2, -1);
            OutputMyList(myList1);

            myList1.Remove(-1);
            myList1.RemoveRange(37, 2);
            myList1.RemoveAt(0);
            myList1.RemoveAll(x => x == 8);
            OutputMyList(myList1);
        }