public void Abstract_Class_constructor_And_Calling_Abstarct_method_Thorugh_Constructor() { //If you want abstract method to be invoked automatically whenever an instance of the class that is derived from abstract class is created, //then we call it in an abstract class constructor CorporateCustomer cc = new CorporateCustomer();//See that the Print method is called automatically SavingsCustomer sc = new SavingsCustomer(); }
/* * 1. List is one of the generic collection classes present in System.Collections.Generic namespace. * 2. There are several generic collection classess are: * a. Dictionary. * b. List. * c. Stack. * d. Queue etc. * * 3. A list class can be used to create a collection of any type. * Ex. we can create a list of integers, strings and even complex types such as class. * 4. The objects stored in the list can be accessed by index. * 5. Unlike arrays, lists can grow in size automatically. * 6. This class also provides methods to search, sort and manipulate lists. * */ static void Main(string[] args) { Customer customer1 = new Customer() { ID = 101, Name = "Anand Dev", Salary = 4000 }; Customer customer2 = new Customer() { ID = 102, Name = "Praveen", Salary = 5000 }; Customer customer3 = new Customer() { ID = 103, Name = "Uttam", Salary = 8000 }; List<Customer> listCustomers = new List<Customer>(2); listCustomers.Add(customer1); listCustomers.Add(customer2); listCustomers.Add(customer3); #region 1. Fetch values from list using index. Console.WriteLine("Fetch values from list using index:"); Customer cust = listCustomers[0]; Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", cust.ID, cust.Name, cust.Salary); #endregion #region 2. Fetch values from list using foreach loop. Console.WriteLine("\nFetch values from list using foreach loop:"); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", c.ID, c.Name, c.Salary); } #endregion #region 3. Lists are stronly typed, it can add object of defined list type or type derived from defined types. Ex. "SavingsCustomer" SavingsCustomer sc = new SavingsCustomer() { ID= 104, Name = "Nanhi", Salary = 6000 }; listCustomers.Add(sc); Console.WriteLine("\nWe can add derived type of object of defined type of list:"); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", c.ID, c.Name, c.Salary); } #endregion #region 4. Insert method in List listCustomers.Insert(1,sc); Console.WriteLine("\nWe have inserted ID = 104 at 1st index as well:"); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", c.ID, c.Name, c.Salary); } #endregion #region 5. IndexOf() method, There are several overloaded versions Console.WriteLine("\nIndex of any item from List:"); int defaultindex = listCustomers.IndexOf(sc); Console.WriteLine("Default index searched object = {0}, for ID = 104, it will give the first index where it will be found.", defaultindex); int specificIndex = listCustomers.IndexOf(sc,2); Console.WriteLine("Specific index of object = {0}, for ID = 104.", specificIndex); #endregion Console.ReadKey(); }
static void Main(string[] args) { CorporateCustomer CC = new CorporateCustomer(); Console.WriteLine(CC.ID); SavingsCustomer SC = new SavingsCustomer(); Console.WriteLine(SC.ID); }
static void Main(string[] args) { Customer customer1 = new Customer() { ID = 101, Name = "Mark", Salary = 5000 }; Customer customer2 = new Customer() { ID = 102, Name = "Pam", Salary = 6500 }; Customer customer3 = new Customer() { ID = 103, Name = "John", Salary = 3500 }; SavingsCustomer savingsCustomer1 = new SavingsCustomer() { ID = 201, Name = "Markus", Salary = 4000 }; List <Customer> customers = new List <Customer>(); customers.Add(customer1); customers.Add(customer2); customers.Add(customer3); customers.Insert(0, savingsCustomer1); //with insert i can choice position foreach (Customer c in customers) { Console.WriteLine("ID = " + c.ID + ", Name = " + c.Name + ", Salary = " + c.Salary); } Console.WriteLine(); for (int i = 0; i < customers.Count; i++) { Customer c = customers[i]; Console.WriteLine("ID = " + c.ID + ", Name = " + c.Name + ", Salary = " + c.Salary); } Console.WriteLine(); Console.WriteLine("Index of customer 1 is: " + customers.IndexOf(customer1)); Console.ReadLine(); }
static void Main(string[] args) { Customer customer1 = new Customer() { ID = 101, Name = "Mark", Salary = 5000 }; Customer customer2 = new Customer() { ID = 102, Name = "Pam", Salary = 6500 }; Customer customer3 = new Customer() { ID = 103, Name = "John", Salary = 3500 }; SavingsCustomer savingsCustomer1 = new SavingsCustomer() { ID = 201, Name = "Markus", Salary = 4000 }; List<Customer> customers = new List<Customer>(); customers.Add(customer1); customers.Add(customer2); customers.Add(customer3); customers.Insert(0, savingsCustomer1); //with insert i can choice position foreach (Customer c in customers) { Console.WriteLine("ID = " + c.ID + ", Name = " + c.Name + ", Salary = " + c.Salary); } Console.WriteLine(); for (int i = 0; i < customers.Count; i++) { Customer c = customers[i]; Console.WriteLine("ID = " + c.ID + ", Name = " + c.Name + ", Salary = " + c.Salary); } Console.WriteLine(); Console.WriteLine("Index of customer 1 is: " + customers.IndexOf(customer1)); Console.ReadLine(); }
static void Main(string[] args) //Classes { Customer customer1 = new Customer() { ID = 101, Name = "Mark", Salary = 5000 }; Customer customer2 = new Customer() { ID = 110, Name = "Pam", Salary = 6500 }; Customer customer3 = new Customer() { ID = 119, Name = "John", Salary = 3500 }; List <Customer> customers = new List <Customer>(2); //KAn bara adda av typen customers...om inte Ärvda klasser... customers.Add(customer1); customers.Add(customer2); customers.Add(customer3); customers.Insert(0, customer3); //Nu kommer custemor 3 vara på två ställen, både på första och sista.. Console.WriteLine(customers.IndexOf(customer3, 1, 3)); //foreach (Customer c in customers) //{ // Console.WriteLine(c.ID); //} SavingsCustomer sc = new SavingsCustomer(); customers.Add(sc); //foreach (Customer c in customers) //for (int i = 0; i < customers.Count; i++) //{ // Customer c = customers[i]; // Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary); // c kan heta vad som helst.... //} }
static void Main(string[] args) { //instantiation of objects Customer c1 = new Customer() { ID = 101, Name = "Abishek", Salary = 2000 }; Customer c2 = new Customer() { ID = 102, Name = "Bikash", Salary = 3000 }; Customer c3 = new Customer() { ID = 103, Name = "Rabin", Salary = 4000 }; List <Customer> customers = new List <Customer>(2); customers.Add(c1);//adding elements customers.Add(c2); customers.Add(c3); customers.Insert(1, c3); //put objects at any index required customers.IndexOf(c1); //finding index of a particular obj Console.WriteLine(customers.IndexOf(c3)); Console.WriteLine(customers.IndexOf(c3, 2)); //finding index of a particular obj, starting from ind 2 Console.WriteLine(customers.IndexOf(c3, 2, 2)); //finding index of a particular obj, starting from ind 2 and lookup only two elements after it. Customer cc = customers[2]; Console.WriteLine("ID: {0}, Name: {1}, Salary: {2}", cc.ID, cc.Name, cc.Salary); Console.WriteLine("---------------------------------------------------"); //for each loop foreach (Customer ccc in customers) { Console.WriteLine("ID: {0}, Name: {1}, Salary: {2}", ccc.ID, ccc.Name, ccc.Salary); } Console.WriteLine("---------------------------------------------------"); //for loop for (int i = 0; i < customers.Count; i++) { Console.WriteLine("ID: {0}, Name: {1}, Salary: {2}", customers[i].ID, customers[i].Name, customers[i].Salary); } SavingsCustomer sc1 = new SavingsCustomer(); customers.Add(sc1);//adding an inherited object Console.WriteLine("---------------------------------------------------"); Console.WriteLine(); //different associated functions if (customers.Contains(c1)) { Console.WriteLine("Found"); } else { Console.WriteLine("Not found"); } if (customers.Exists(cust => cust.Name.StartsWith("B")))//specifying a condition to find { Console.WriteLine("Exists"); } else { Console.WriteLine("Doesnt exists"); } Customer cn = customers.Find(cust => cust.Salary > 2000);//gives the first matching element , according to condition Console.WriteLine("ID: {0}, Name: {1}, Salary: {2}", cn.ID, cn.Name, cn.Salary); Customer cnn = customers.FindLast(cust => cust.Salary > 2000);//gives the last matching element , according to condition Console.WriteLine("ID: {0}, Name: {1}, Salary: {2}", cnn.ID, cnn.Name, cnn.Salary); Console.WriteLine("---------------------------------------------------"); List <Customer> custs = customers.FindAll(cust => cust.Salary > 500);//gives a list of obj , according to condition! foreach (Customer cuu in custs) { Console.WriteLine("ID: {0}, Name: {1}, Salary: {2}", cuu.ID, cuu.Name, cuu.Salary); } int ind = customers.FindIndex(cust => cust.Salary == 2000); Console.WriteLine(ind); //overloads int indx = customers.FindIndex(0, cust => cust.Salary == 2000); int indxx = customers.FindIndex(0, 2, cust => cust.Salary == 2000); //same goes with FindLastIndex(); //conversion of Lists: Customer[] cusss = new Customer[3]; cusss[0] = c1; cusss[1] = c2; cusss[2] = c3; List <Customer> List1 = cusss.ToList(); //array to list customers.ToArray(); //list to array Dictionary <int, Customer> dic1 = List1.ToDictionary(x => x.ID); //list to dictionary }
/* * 1. List is one of the generic collection classes present in System.Collections.Generic namespace. * 2. There are several generic collection classess are: * a. Dictionary. * b. List. * c. Stack. * d. Queue etc. * * 3. A list class can be used to create a collection of any type. * Ex. we can create a list of integers, strings and even complex types such as class. * 4. The objects stored in the list can be accessed by index. * 5. Unlike arrays, lists can grow in size automatically. * 6. This class also provides methods to search, sort and manipulate lists. * */ static void Main(string[] args) { Customer customer1 = new Customer() { ID = 101, Name = "Anand Dev", Salary = 4000 }; Customer customer2 = new Customer() { ID = 102, Name = "Praveen", Salary = 5000 }; Customer customer3 = new Customer() { ID = 103, Name = "Uttam", Salary = 8000 }; List <Customer> listCustomers = new List <Customer>(2); listCustomers.Add(customer1); listCustomers.Add(customer2); listCustomers.Add(customer3); #region 1. Fetch values from list using index. Console.WriteLine("Fetch values from list using index:"); Customer cust = listCustomers[0]; Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", cust.ID, cust.Name, cust.Salary); #endregion #region 2. Fetch values from list using foreach loop. Console.WriteLine("\nFetch values from list using foreach loop:"); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", c.ID, c.Name, c.Salary); } #endregion #region 3. Lists are stronly typed, it can add object of defined list type or type derived from defined types. Ex. "SavingsCustomer" SavingsCustomer sc = new SavingsCustomer() { ID = 104, Name = "Nanhi", Salary = 6000 }; listCustomers.Add(sc); Console.WriteLine("\nWe can add derived type of object of defined type of list:"); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", c.ID, c.Name, c.Salary); } #endregion #region 4. Insert method in List listCustomers.Insert(1, sc); Console.WriteLine("\nWe have inserted ID = 104 at 1st index as well:"); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salry = {2}", c.ID, c.Name, c.Salary); } #endregion #region 5. IndexOf() method, There are several overloaded versions Console.WriteLine("\nIndex of any item from List:"); int defaultindex = listCustomers.IndexOf(sc); Console.WriteLine("Default index searched object = {0}, for ID = 104, it will give the first index where it will be found.", defaultindex); int specificIndex = listCustomers.IndexOf(sc, 2); Console.WriteLine("Specific index of object = {0}, for ID = 104.", specificIndex); #endregion Console.ReadKey(); }
public ListCollection() { Customer10 c1 = new Customer10() { ID = 101, Name = "Mark", Salary = 5000, Type = "RetailCustomer" }; Customer10 c2 = new Customer10() { ID = 110, Name = "Pam", Salary = 6500, Type = "RetailCustomer" }; Customer10 c3 = new Customer10() { ID = 119, Name = "John", Salary = 3500, Type = "RetailCustomer" }; Customer10 c4 = new Customer10() { ID = 119, Name = "Rob", Salary = 6500, Type = "CorporateCustomer" }; Customer10 c5 = new Customer10() { ID = 119, Name = "Sam", Salary = 3500, Type = "CorporateCustomer" }; // array can not grow. It stays with 2 members how he was declared Customer10[] customersArr = new Customer10[2]; customersArr[0] = c1; customersArr[1] = c2; //customersArr[2] = c3; index outside the bound of an array // list will grow in size automatically even if it is declared with a capacity List <Customer10> customerList = new List <Customer10>(2); customerList.Add(c1); customerList.Add(c2); customerList.Add(c3); SavingsCustomer sc = new SavingsCustomer(); customerList.Add(sc); // Insert at specific position //customerList.Insert(0, c3); // index of first item in collection int indexC = customerList.IndexOf(c3); // search stating from a defined position int indexC1 = customerList.IndexOf(c3, 1); // only look in first 2 items of the List // 2 must be <= count of list items -> error otherwise (outOfRange) int indexCN = customerList.IndexOf(c3, 1, 2); // -1 = not found //CONTAINS bool check if item c2 exits in the list if (customerList.Contains(c2)) { Console.WriteLine("List contains c2 obj"); } //EXISTS check if contains in the list based on a function if (customerList.Exists(cust => cust.Name.StartsWith("P"))) { Console.WriteLine("List contains customers for which Name hat start with 'P'"); } //FIND only returns first item(Customer10 obj) Customer10 cfind = customerList.Find(cust => cust.Salary > 5000); // find last item that match condition Customer10 cfindl = customerList.FindLast(cust => cust.Salary > 5000); // find all items that match condition List <Customer10> cfindAll = customerList.FindAll(cust => cust.Salary > 5000); // convert array to List List <Customer10> listCust = customersArr.ToList(); customerList.ToArray(); Dictionary <int, Customer10> dictList = customerList.ToDictionary(x => x.ID); foreach (KeyValuePair <int, Customer10> kvp in dictList) { Customer10 c10 = kvp.Value; Console.WriteLine("Dictionary Key={0} ID={1} Name={2} Salary={3}", kvp.Key, c10.ID, c10.Name, c10.Salary); } int indexFind = 0; // find position of first item that match the description indexFind = customerList.FindIndex(cust => cust.Salary > 5000); indexFind = customerList.FindIndex(2, cust => cust.Salary > 5000); // starting from position 2 indexFind = customerList.FindLastIndex(cust => cust.Salary > 5000); // last index that match condition Console.WriteLine("FIND ID={0} Name={1} Salary={2} indexC={3}", cfind.ID, cfind.Name, cfind.Salary, indexC); Customer10 c = customerList[0]; Console.WriteLine("ID={0} Name={1} Salary={2} indexC={3}", c.ID, c.Name, c.Salary, indexC); foreach (Customer10 cf in cfindAll) { Console.WriteLine("cfindAll ID={0} Name={1} Salary={2}", cf.ID, cf.Name, cf.Salary); } List <Customer10> customerListCorporate = new List <Customer10>(); customerListCorporate.Add(c4); customerListCorporate.Add(c5); //ADDRANGE add second list items to first list customerList.AddRange(customerListCorporate); //GETRANGE get list of elements List <Customer10> custr = customerList.GetRange(3, 2);// from index3 i want 2 items foreach (Customer10 cf in custr) { Console.WriteLine("GetRange ID={0} Name={1} Salary={2}", cf.ID, cf.Name, cf.Salary); } //INSERTRANGE insert items at specific position customerList.InsertRange(3, customerListCorporate); // remove only one item(object) customerList.Remove(c1); customerList.RemoveAt(4); // remove index at position 4(5'th element) customerList.RemoveAll(x => x.Type == "CorporateCustomers"); // remove all with condition customerList.RemoveRange(3, 2); // from index3 remove 2 items for (int i = 0; i < customerList.Count; i++) { Customer10 cf = customerList[i]; Console.WriteLine("ID={0} Name={1} Salary={2}", cf.ID, cf.Name, cf.Salary); } //COPY content of a list to another List <Customer10> customersAll = customerList.ToList(); //SORT List <int> numbers = new List <int>() { 1, 6, 8, 7, 5, 2, 3, 4 }; numbers.Sort(); numbers.Reverse(); foreach (int number in numbers) { Console.Write("{0},", number); } // must implement IComparable to work //customerList.Sort(); List <Customer10> customer11List = customerList.ToList(); customer11List.Sort(); //SORT custom by IComparable SortByName sortByName = new SortByName(); customer11List.Sort(sortByName); //SORT by DELEGATE (A) Comparison <Customer10> customComparer = new Comparison <Customer10>(CompareCustomer); customer11List.Sort(customComparer); //SORT by DELEGATE (B) simpler customer11List.Sort(delegate(Customer10 ct1, Customer10 ct2) { return(ct1.ID.CompareTo(ct2.ID)); }); //SORT by LAMBDA expression (C) simpler customer11List.Sort((x, y) => x.ID.CompareTo(y.ID)); foreach (Customer10 cf in customer11List) { Console.WriteLine("SORT ID={0} Name={1} Salary={2}", cf.ID, cf.Name, cf.Salary); } //METHODS //TrueForAll - condition testing for all members Console.WriteLine("Are all salaries > 5000 ? answer is:{0}", customer11List.TrueForAll(x => x.Salary > 5000)); //AsReadOnly - can not be modified ReadOnlyCollection <Customer10> readonlyCustomers = customer11List.AsReadOnly(); //TrimExcell - minimize memory footprint (90% treshhold) List <int> numbersTrim = new List <int>(10) { 1, 6 }; numbersTrim.TrimExcess(); Console.WriteLine("Capacity:{0}", numbersTrim.Capacity); }
static void Main(string[] args) { Customer customer1 = new Customer { ID = 101, Name = "Mark", Salary = 5000, }; Customer customer2 = new Customer { ID = 119, Name = "John", Salary = 3500 }; Customer customer3 = new Customer { ID = 110, Name = "Pam", Salary = 6500 }; List <Customer> customers = new List <Customer>(2); // List<> is strongly typed. This is type of Customer. customers.Add(customer1); customers.Add(customer2); customers.Add(customer3); // List can change the size customers.Insert(0, customer3); foreach (Customer c in customers) { Console.WriteLine(c.ID); } Console.WriteLine(customers.IndexOf(customer3)); // 0 is the result // the result will be 3 Console.WriteLine(customers.IndexOf(customer3, 1, 3)); // the second parameter means which position you want start to search, // the third parameter means how many items you want to search foreach (Customer c in customers) { Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary); } for (int i = 0; i < customers.Count; i++) { Customer c = customers[i]; Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary); } SavingsCustomer sr = new SavingsCustomer(); // SavingsCustomer is inherited from Customer class. customers.Add(sr); // So it is okay to add sr. it is still strongly typed. Console.WriteLine("-------------------------------------"); // check if customer is in the list if (customers.Contains(customer3)) { Console.WriteLine("Customer 3 is in the list"); } else { Console.WriteLine("Customer 3 is not in the list"); } // use lambda expression if (customers.Exists(cust => cust.Name.StartsWith("P"))) { Console.WriteLine("Customer3 object exists in the list"); } else { Console.WriteLine("Customer3 object does not exist in the list"); } // Find() returns one object Customer c2 = customers.Find(cust => cust.Salary > 5000); Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c2.ID, c2.Name, c2.Salary); //FindAll() // Convert an array to list Customer[] customerArray = new Customer[3]; customerArray[0] = customer1; customerArray[1] = customer2; customerArray[2] = customer3; List <Customer> listCustomers = customerArray.ToList(); foreach (Customer c in listCustomers) { Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary); } // convert a list to a dictionary Dictionary <int, Customer> dictionary = listCustomers.ToDictionary(x => x.ID); foreach (KeyValuePair <int, Customer> kvp in dictionary) { Customer c = kvp.Value; Console.WriteLine("ID = {0}, Name = {1}, Salary = {2}", c.ID, c.Name, c.Salary); } Console.ReadLine(); }
static void Main(string[] args) { SavingsCustomer sc = new SavingsCustomer(); CorporateCustomer cc = new CorporateCustomer(); }
static void Main(string[] args) { Customer customer1 = new Customer() { ID = 101, Name = "Mark", Salary = 5000 }; Customer customer2 = new Customer() { ID = 110, Name = "Pam", Salary = 6500 }; Customer customer3 = new Customer() { ID = 119, Name = "John ", Salary = 7000 }; /*Array: size of array is two and if we give it more than two arrays it will give us error*/ Customer[] customersArray = new Customer[2]; customersArray [0] = customer1; customersArray [1] = customer2; //customersArray [2] = customer3; /*List: Size Increases automatically*/ List <Customer> customerList = new List <Customer>(2); customerList.Add(customer1); customerList.Add(customer2); customerList.Add(customer3); /*we can retrieve customer by the list indexes like:*/ Customer c = customerList[0]; Console.WriteLine("Customer Details: " + "ID: " + c.ID); /*Looping through each customers*/ /*foreach (var item in collection) * { * syntax of foreach loop * }*/ foreach (Customer customer in customerList) { Console.WriteLine("Customer Details: " + "ID: " + customer.ID); } for (int i = 0; i <= customerList.Count - 1; i++) { Console.WriteLine("Customer Details" + "ID: " + customerList[i].ID); } SavingsCustomer sc = new SavingsCustomer(); /*since Savings Customer is inherting from Customer class I can add it in List<Customers> , that is the list of type customer * so Customer and Saving Customers are closesly tied*/ List <Customer> customerSavingsList = new List <Customer>(2); customerSavingsList.Add(sc); /*we can also insert an element in list at a specific position or index, */ customerList.Insert(10, customer2); foreach (Customer customer in customerList) { Console.WriteLine("Customer Details: " + "ID: " + customer.ID); } /* You can also find the index of specific object in list*/ /*if we try to find the index of element which has been added twice to the list, it returns the first index at which the object exists*/ Console.WriteLine("Index number of a customer which has been added twice: " + customerList.IndexOf(customer2)); /*you can also start looking from cetrain index number using overloading in Index of Method*/ Console.WriteLine("Index number of a customer which has been added twice: " + customerList.IndexOf(customer2, 2)); }