Example #1
0
        static void Main(string[] args)
        {
            ObservableCollections.OCMain();


            //var names = new List<string>(6)
            var names = new List <string>
            {
                "DVS Ram",
                "Aditya",
                "Srinath"
            };

            Console.WriteLine($"Count : {names.Count} Capacity : {names.Capacity}");
            names.Add("Kishor");
            Console.WriteLine($"Count : {names.Count} Capacity : {names.Capacity}");
            names.Add("kira");
            Console.WriteLine($"Count : {names.Count} Capacity : {names.Capacity}");

            //index=6
            //names.RemoveAt(6);
            //removes one element that matchs input string
            //names.Remove("kira");
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }

            //if list is big copying to array takes long
            //string[] copy = names.ToArray();
            //var copy = new ReadOnlyCollection<string>(names);
            var copy = names.AsReadOnly();

            //BadCode(copy);


            var list = new NonBlankStringList();

            list.Add("kishor");
            list[0] = "";
            list.Insert(1, "kk");
            foreach (var name in list)
            {
                Console.WriteLine(name);
            }
        }
Example #2
0
        static void NonBlankStringListPractice()
        {
            NonBlankStringList list = new NonBlankStringList();

            try
            {
                list.Add("   ");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            list[0] = "item 0 changed";
            list.Add("item 1");
            list.Insert(2, "item 2 inserted");

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var presidents = new List <string> {
                "Jimmy Carter",
                "Ronald Reagan",
                "George HW Bush",
                "Bill Clinton",
                "George W Bush",
                "Barack Obama"
            };

            foreach (string president in presidents)
            {
                Console.WriteLine(president);
            }

            Console.WriteLine("Count = " + presidents.Count);
            Console.WriteLine("Capacity = " + presidents.Capacity);

            Console.WriteLine("\nAdd president");
            presidents.Add("Donald Trump");

            foreach (string president in presidents)
            {
                Console.WriteLine(president);
            }

            Console.WriteLine("Count = " + presidents.Count);
            Console.WriteLine("Capacity = " + presidents.Capacity);

            Console.WriteLine("\nRemove president");
            presidents.RemoveAt(5);

            foreach (string president in presidents)
            {
                Console.WriteLine(president);
            }

            Console.WriteLine("Count = " + presidents.Count);
            Console.WriteLine("Capacity = " + presidents.Capacity);

            Console.WriteLine("\nList as readonly");
            try{
                TryToRemoveFromList(presidents.AsReadOnly());
            }catch (Exception ex) {
                Console.WriteLine("Can't alter read only list: " + ex.Message);
            }

            Console.WriteLine("\nCollection<T> designed for inheritance. Don't allow empty strings in list");
            var lst = new NonBlankStringList
            {
                "Item added at index 0",
                "Item added at index 1"
            };

            try{
                lst[0] = "   ";
            }catch (ArgumentException ex) {
                Console.WriteLine("When try to add '    ' item to the list: " + ex.Message);
            }
            lst.Insert(2, "Item inserted at index 2");

            foreach (string item in lst)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\nObservableCollection  - list that provides change notifications");
            var p = new ObservableCollection <string>
            {
                "Jimmy Carter",
                "Ronald Reagan",
                "George HW Bush"
            };

            p.CollectionChanged += OnCollectionChanged;

            p.Add("Bill Clinton");
            p.Remove("Jimmy Carter");

            foreach (string president in p)
            {
                Console.WriteLine(president);
            }
        }