Beispiel #1
0
        static void Main(string[] args)
        {
            List <string> list = new List <string>();

            list.Add("Maria");
            list.Add("Alex");
            list.Add("Bob");
            list.Add("Anna");
            list.Add("Jacob");
            list.Add("Sarah");
            list.Insert(2, "Marco");

            Console.WriteLine("List Foreach: ");
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("\nList Count: " + list.Count);

            string search1 = list.Find(x => x[0] == 'A');

            Console.WriteLine("\nFirst 'A': " + search1);

            string search2 = list.FindLast(x => x[0] == 'A');

            Console.WriteLine("\nLast 'A': " + search2);

            int position1 = list.FindIndex(x => x[0] == 'A');

            Console.WriteLine("\nFirst position: " + position1);

            int position2 = list.FindLastIndex(x => x[0] == 'A');

            Console.WriteLine("\nLast position: " + position2);

            Console.WriteLine("\nFind All: ");
            List <string> list2 = list.FindAll(x => x.Length == 5);

            foreach (string obj in list2)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("\nList Remove: ");
            list.Remove("Alex");
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("\nList Remove At: ");
            list.RemoveAt(3);
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("\nList Remove Range: ");
            list.RemoveRange(2, 1);
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("\nList Remove At: ");
            list.RemoveAt(0);
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("\nList Remove All: ");
            list.RemoveAll(x => x[0] == 'M');
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }
        }
Beispiel #2
0
        public static void Main()
        {
            Customer customer1 = new Customer()
            {
                ID     = 101,
                Name   = "Mark",
                Salary = 4000
            };

            Customer customer2 = new Customer()
            {
                ID     = 102,
                Name   = "Pam",
                Salary = 7000
            };

            Customer customer3 = new Customer()
            {
                ID     = 104,
                Name   = "Rob",
                Salary = 5500
            };

            Customer[] arrayCustomers = new Customer[3];
            arrayCustomers[0] = customer1;
            arrayCustomers[1] = customer2;
            arrayCustomers[2] = customer3;

            List <Customer> listCustomers = arrayCustomers.ToList();

            foreach (Customer c in listCustomers)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary);
            }
            Console.WriteLine("------------------------------------------------------");

            Customer[] arrayAllCustomers = listCustomers.ToArray();
            foreach (Customer c in arrayAllCustomers)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary);
            }
            Console.WriteLine("------------------------------------------------------");


            Dictionary <int, Customer> dictionaryCustomers = listCustomers.ToDictionary(x => x.ID);

            foreach (KeyValuePair <int, Customer> keyValuePairCustomers in dictionaryCustomers)
            {
                Console.WriteLine("Key = {0}", keyValuePairCustomers.Key);
                Customer c = keyValuePairCustomers.Value;
                Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary);
            }
            Console.WriteLine("------------------------------------------------------");


            if (listCustomers.Contains(customer2))
            {
                Console.WriteLine("Customer2 object exists in the list");
            }
            else
            {
                Console.WriteLine("Customer2 object does not exist in the list");
            }
            Console.WriteLine("------------------------------------------------------");
            if (listCustomers.Exists(x => x.Name.StartsWith("M")))
            {
                Console.WriteLine("List contains customer whose name starts with M");
            }
            else
            {
                Console.WriteLine("List does not contain a customer whose name starts with M");
            }
            Console.WriteLine("------------------------------------------------------");
            Customer cust = listCustomers.Find(customer => customer.Salary > 5000);

            Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", cust.ID, cust.Name, cust.Salary);
            Console.WriteLine("------------------------------------------------------");
            Customer lastMatch = listCustomers.FindLast(customer => customer.Salary > 5000);

            Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", lastMatch.ID, lastMatch.Name, lastMatch.Salary);
            Console.WriteLine("------------------------------------------------------");
            List <Customer> filteredCustomers = listCustomers.FindAll(customer => customer.Salary > 5000);

            foreach (Customer cstmr in filteredCustomers)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", cstmr.ID, cstmr.Name, cstmr.Salary);
            }
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Index of the first matching customer object whose salary is greater 5000 =" +
                              listCustomers.FindIndex(customer => customer.Salary > 5000));
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine("Index of the Last matching customer object whose salary is greater 5000 = " +
                              listCustomers.FindLastIndex(customer => customer.Salary > 5000));
            Console.WriteLine("------------------------------------------------------");
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            //Exemplos:

            List <string> ex  = new List <string>();
            List <string> ex2 = new List <string> {
                "Igor", "Fran", "João"
            };

            //---------------------------------------------------------------

            List <string> list = new List <string>();

            list.Add("Maria");                   //Add
            list.Add("Alex");
            list.Add("Bob");
            list.Add("Anna");
            list.Insert(2, "Marco");             //Insert
            list.Add("Igor");
            list.Add("Vergo");
            list.Add("Luffy");

            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("List Count: " + list.Count);                               //Count

            string s1 = list.Find(x => x[0] == 'A');                                      //Finds

            Console.WriteLine("First beginning with A: " + s1);

            string s2 = list.FindLast(x => x[0] == 'A');

            Console.WriteLine("Last beginning with A: " + s2);

            int pos1 = list.FindIndex(x => x[0] == 'A');

            Console.WriteLine("First position name beginning with A: " + pos1);

            int pos2 = list.FindLastIndex(x => x[0] == 'A');

            Console.WriteLine("Last position name beginning with A: " + pos2);

            Console.WriteLine("-------------------------------");

            List <string> list2 = list.FindAll(x => x.Length == 5);

            foreach (string obj in list2)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("-------------------------------");
            list.Remove("Marco");                                                         //Removes
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("-------------------------------");

            list.RemoveAll(x => x[0] == 'M');
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("-------------------------------");

            list.RemoveAt(1);
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("-------------------------------");

            list.RemoveRange(2, 2);
            foreach (string obj in list)
            {
                Console.WriteLine(obj);
            }
        }