Your SlideShare is downloading. ×
C# Starter L04-Collections
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

C# Starter L04-Collections

785
views

Published on

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
785
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
131
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L04 – Collections
  • 2. Collections
  • 3. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } }
  • 4. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } }
  • 5. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } }
  • 6. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } }
  • 7. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } }
  • 8. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } }
  • 9. Collections using System; using System.Collections.Generic; namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { List<int> myInts = new List<int>(); myInts.Add(1); myInts.Add(2); myInts.Add(3); for (int i = 0; i < myInts.Count; i++) { Console.WriteLine("MyInts: {0}", myInts[i]); } } } } MyInts: 1 MyInts: 2 MyInts: 3 Press any key to continue...
  • 10. Collections - Dictionaries • Let’s have the following class namespace ConsoleApplicationCourseTest { public class Customer { public Customer(int id, string name) { _id = id; _name = name; } private int _id; public int ID { get { return _id; } set { _id = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } } }
  • 11. Collections • And have the following main namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, Customer> customers = new Dictionary<int, Customer>(); Customer cust1 = new Customer(1, "Cust 1"); Customer cust2 = new Customer(2, "Cust 2"); Customer cust3 = new Customer(3, "Cust 3"); customers.Add(cust1.ID, cust1); customers.Add(cust2.ID, cust2); customers.Add(cust3.ID, cust3); foreach (KeyValuePair<int, Customer> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value.Name); } } } }
  • 12. Collections namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, Customer> customers = new Dictionary<int, Customer>(); Customer cust1 = new Customer(1, "Cust 1"); Customer cust2 = new Customer(2, "Cust 2"); Customer cust3 = new Customer(3, "Cust 3"); customers.Add(cust1.ID, cust1); customers.Add(cust2.ID, cust2); customers.Add(cust3.ID, cust3); foreach (KeyValuePair<int, Customer> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value.Name); } } } }
  • 13. Collections namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, Customer> customers = new Dictionary<int, Customer>(); Customer cust1 = new Customer(1, "Cust 1"); Customer cust2 = new Customer(2, "Cust 2"); Customer cust3 = new Customer(3, "Cust 3"); customers.Add(cust1.ID, cust1); customers.Add(cust2.ID, cust2); customers.Add(cust3.ID, cust3); foreach (KeyValuePair<int, Customer> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value.Name); } } } }
  • 14. Collections namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, Customer> customers = new Dictionary<int, Customer>(); Customer cust1 = new Customer(1, "Cust 1"); Customer cust2 = new Customer(2, "Cust 2"); Customer cust3 = new Customer(3, "Cust 3"); customers.Add(cust1.ID, cust1); customers.Add(cust2.ID, cust2); customers.Add(cust3.ID, cust3); foreach (KeyValuePair<int, Customer> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value.Name); } } } } Customer ID: 1, Name: Cust 1 Customer ID: 2, Name: Cust 2 Customer ID: 3, Name: Cust 3 Press any key to continue...
  • 15. Collections namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, string> customers = new Dictionary<int, string>(); for (int i = 0; i < 3; i++) { customers.Add(i,"Cust" + i); } foreach (KeyValuePair<int, string> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value); } Console.WriteLine(); customers[1] = "ZGTR"; foreach (KeyValuePair<int, string> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value); } } } }
  • 16. Collections namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, string> customers = new Dictionary<int, string>(); for (int i = 0; i < 3; i++) { customers.Add(i,"Cust" + i); } foreach (KeyValuePair<int, string> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value); } Console.WriteLine(); customers[1] = "ZGTR"; foreach (KeyValuePair<int, string> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value); } } } }
  • 17. Collections namespace ConsoleApplicationCourseTest { class MainClass { public static void Main(String[] args) { Dictionary<int, string> customers = new Dictionary<int, string>(); for (int i = 0; i < 3; i++) { customers.Add(i,"Cust" + i); } foreach (KeyValuePair<int, string> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value); } Console.WriteLine(); customers[1] = "ZGTR"; foreach (KeyValuePair<int, string> custKeyVal in customers) { Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value); } } } } Customer ID: 0, Name: Cust0 Customer ID: 1, Name: Cust1 Customer ID: 2, Name: Cust2 Customer ID: 0, Name: Cust0 Customer ID: 1, Name: ZGTR Customer ID: 2, Name: Cust2 Press any key to continue...
  • 18. Collections • System.Collections.Generic • System.Collections
  • 19. Collections • foreach danger – NEVER EVER manipulate (insertion, deletion) a collection in a “foreach” statement – Just iterate (and/or update if you want.) – But Why?
  • 20. Collections private void button1_Click(object sender, EventArgs e) { System.Collections.ArrayList arrList = new ArrayList(); arrList.Add(new Button()); arrList.Add(new TextBox()); arrList.Add(new ComboBox()); arrList.Add(new ListBox()); foreach (var item in arrList) { arrList.Remove(item); } }
  • 21. Collections private void Test(){ System.Collections.ArrayList arrList = new ArrayList(); arrList.Add(new Button()); arrList.Add(new TextBox()); arrList.Add(new ComboBox()); arrList.Add(new ListBox()); foreach (var item in arrList) { arrList.Remove(item); } }
  • 22. Collections
  • 23. Collections private void Test(){ { System.Collections.ArrayList arrList = new ArrayList(); arrList.Add(new Button()); arrList.Add(new TextBox()); arrList.Add(new ComboBox()); arrList.Add(new ListBox()); arrList.Clear(); } Clearing all the collection!
  • 24. Collections
  • 25. Collections
  • 26. Collections
  • 27. Collections
  • 28. Collections
  • 29. Collections
  • 30. Collections
  • 31. Collections
  • 32. Collections
  • 33. Collections
  • 34. Collections
  • 35. Collections
  • 36. Collections public static void Main(String[] args) { List<int> list1 = new List<int> { 1, 2, 3, 4, 5 }; foreach (var i in list1) { Console.Write(i + ", "); } Console.WriteLine(); List<int> list2 = list1.ConvertAll(x => 2 * x); foreach (var i in list2) { Console.Write(i + ", "); } }
  • 37. Collections public static void Main(String[] args) { List<int> list1 = new List<int> { 1, 2, 3, 4, 5 }; foreach (var i in list1) { Console.Write(i + ", "); } Console.WriteLine(); List<int> list2 = list1.ConvertAll(x => 2 * x); foreach (var i in list2) { Console.Write(i + ", "); } }
  • 38. Collections 1, 2, 3, 4, 5, 2, 4, 6, 8, 10, Press any key to continue... public static void Main(String[] args) { List<int> list1 = new List<int> { 1, 2, 3, 4, 5 }; foreach (var i in list1) { Console.Write(i + ", "); } Console.WriteLine(); List<int> list2 = list1.ConvertAll(x => 2 * x); foreach (var i in list2) { Console.Write(i + ", "); } }
  • 39. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 40. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 41. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 42. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 43. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 44. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 45. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 46. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); }
  • 47. Collections public static void Main() { List<PointF> lpf = new List<PointF>(); lpf.Add(new PointF(27.8F, 32.62F)); lpf.Add(new PointF(99.3F, 147.273F)); lpf.Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach (PointF p in lpf) { Console.WriteLine(p); } List<Point> lp = lpf.ConvertAll(new Converter<PointF, Point>(PointFToPoint)); Console.WriteLine(); foreach (Point p in lp) { Console.WriteLine(p); } } public static Point PointFToPoint(PointF pf) { return new Point(((int)pf.X), ((int)pf.Y)); } {X=27.8, Y=32.62} {X=99.3, Y=147.273} {X=7.5, Y=1412.2} {X=27,Y=32} {X=99,Y=147} {X=7,Y=1412}
  • 48. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name);
  • 49. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name);
  • 50. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name);
  • 51. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name);
  • 52. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name);
  • 53. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name);
  • 54. Collections string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; Random random = new Random(DateTime.Now.Millisecond); string name = names.ElementAt(random.Next(0, names.Length)); Console.WriteLine("The name chosen at random is '{0}'.", name); The name chosen at random is 'Ito, Shu'.
  • 55. Collections
  • 56. Collections
  • 57. Collections
  • 58. Collections • Find, FindAll public List<T> FindAll( Predicate<T> match )
  • 59. Collections // Search predicate returns true if a string ends in "saurus". private static bool EndsWithSaurus(String s) { if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus")) { return true; } else { return false; } }
  • 60. Collections public static void Main() { List<string> dinosaurs = new List<string> {"Velociraptor", "Deinonychus", "Dilophosaurus"}; Console.WriteLine("TrueForAll(EndsWithSaurus): {0}", dinosaurs.TrueForAll(EndsWithSaurus)); Console.WriteLine("nFind(EndsWithSaurus): {0}", dinosaurs.Find(EndsWithSaurus)); Console.WriteLine("nFindLast(EndsWithSaurus): {0}", dinosaurs.FindLast(EndsWithSaurus)); Console.WriteLine("nFindAll(EndsWithSaurus):"); List<string> sublist = dinosaurs.FindAll(EndsWithSaurus); foreach (string dinosaur in sublist) { Console.WriteLine(dinosaur); } Console.WriteLine("n{0} elements removed by RemoveAll(EndsWithSaurus).", dinosaurs.RemoveAll(EndsWithSaurus)); Console.WriteLine("nList now contains:"); foreach (string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("nExists(EndsWithSaurus): {0}", dinosaurs.Exists(EndsWithSaurus)); }
  • 61. Collections // Search predicate returns true if a string ends in "saurus". private static bool EndsWithSaurus(String s) { if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus")) { return true; } else { return false; } }
  • 62. Collections TrueForAll(EndsWithSaurus): False Find(EndsWithSaurus): Dilophosaurus FindLast(EndsWithSaurus): Dilophosaurus FindAll(EndsWithSaurus): Dilophosaurus 1 elements removed by RemoveAll(EndsWithSaurus). List now contains: Velociraptor Deinonychus Exists(EndsWithSaurus): False Press any key to continue...
  • 63. Collections 1: public sealed class Employee 2: { 3: public long Id { get; set; } 4: public string Name { get; set; } 5: public double Salary { get; set; } 6: } 1: // empty 2: var noEmployeeList = new List<Employee>(); 3: 4: // many items 5: var employees = new List<Employee> 6: { 7: new Employee { Id = 1, Name = "Jim Smith", Salary = 12345.50 }, 8: new Employee { Id = 7, Name = "Jane Doe", Salary = 31234.50 }, 9: new Employee { Id = 9, Name = "John Doe", Salary = 13923.99 }, 10: new Employee { Id = 13, Name = "Jim Smith", Salary = 30123.49 }, 11: //... etc... 12: };
  • 64. Collections 1: public sealed class Employee 2: { 3: public long Id { get; set; } 4: public string Name { get; set; } 5: public double Salary { get; set; } 6: } 1: // empty 2: var noEmployeeList = new List<Employee>(); 3: 4: // many items 5: var employees = new List<Employee> 6: { 7: new Employee { Id = 1, Name = "Jim Smith", Salary = 12345.50 }, 8: new Employee { Id = 7, Name = "Jane Doe", Salary = 31234.50 }, 9: new Employee { Id = 9, Name = "John Doe", Salary = 13923.99 }, 10: new Employee { Id = 13, Name = "Jim Smith", Salary = 30123.49 }, 11: //... etc... 12: }; A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class. It is not permitted to use the abstract modifier with a sealed class. Structs are implicitly sealed; therefore, they cannot be inherited
  • 65. Collections • Like First(), the Last() method will throw if there are no items (or no matches in the case of predicates) in the enumerable: • The LastOrDefault() methods will return a default(TSource) if the list is empty or no matches are found: 1: // throws at runtime because empty enumerable. 2: var empty = noEmployeeList.Last(); 3: 4: // this line will throw at runtime because there is no item that matches 5: // even though the enumerable itself is not empty 6: var noMatch = employees.Last(e => e.Id == 20); 1: // returns default(Employee) -- null -- because list is empty 2: var empty = noEmployeeList.LastOrDefault(); 3: 4: // returns default(Employee) -- null -- because no item matches predicate. 5: var noMatch = employees.Last(e => e.Id == 20);
  • 66. Collections • ElementAt() 1: 2: // returns the second employee (index == 1) which is Jane Doe, Id == 7 3: var janeDoe = employees.ElementAt(1); 4: 5: // since there's not 30 employees, this one will throw exception 6: var willThrow = employees.ElementAt(30); 7: 8: // returns null because there aren't 30 employees, but we used the OrDefault flavor. 9: var noMatch = employees.ElementAtOrDefault(30);
  • 67. Collections • Stack public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine("myStack"); Console.WriteLine("tCount: {0}", myStack.Count); Console.Write("tValues:"); PrintValues(myStack); } public static void PrintValues(IEnumerable myCollection) { foreach (Object obj in myCollection) Console.Write(" {0}", obj); Console.WriteLine(); } }
  • 68. Collections • Stack public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine("myStack"); Console.WriteLine("tCount: {0}", myStack.Count); Console.Write("tValues:"); PrintValues(myStack); } public static void PrintValues(IEnumerable myCollection) { foreach (Object obj in myCollection) Console.Write(" {0}", obj); Console.WriteLine(); } } myStack Count: 3 Values: ! World Hello Press any key to continue...
  • 69. Play more with Collections
  • 70. More Collections Functionalities • Let’s have the following class public class PointClass { public float XLocation { set; get; } public float YLocation { set; get; } public PointClass(float xLocation, float yLocation) { this.XLocation = xLocation; this.YLocation = yLocation; } public override string ToString() { return String.Format("X = {0}, Y = {1}", this.XLocation, this.YLocation); } }
  • 71. More Collections Functionalities public class IsTest { public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; PrintToConsole(listOfPoints, "Before Sorting"); listOfPoints.Sort(); PrintToConsole(listOfPoints, "After default sorting"); } }
  • 72. More Collections Functionalities public class IsTest { public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; PrintToConsole(listOfPoints, "Before Sorting"); listOfPoints.Sort(); PrintToConsole(listOfPoints, "After default sorting"); } }
  • 73. More Collections Functionalities
  • 74. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static int SortByXLocation(PointClass point1, PointClass point2) { if (point1.XLocation > point2.XLocation) return +1; else if (point1.XLocation < point2.XLocation) return -1; else return 0; } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; PrintToConsole(listOfPoints, "Before sorting"); listOfPoints.Sort(SortByXLocation); PrintToConsole(listOfPoints, "After our cool sorting!"); }
  • 75. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static int SortByXLocation(PointClass point1, PointClass point2) { if (point1.XLocation > point2.XLocation) return +1; else if (point1.XLocation < point2.XLocation) return -1; else return 0; } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; PrintToConsole(listOfPoints, "Before sorting"); listOfPoints.Sort(SortByXLocation); PrintToConsole(listOfPoints, "After our cool sorting!"); }
  • 76. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static int SortByXLocation(PointClass point1, PointClass point2) { if (point1.XLocation > point2.XLocation) return +1; else if (point1.XLocation < point2.XLocation) return -1; else return 0; } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; PrintToConsole(listOfPoints, "Before sorting"); listOfPoints.Sort(SortByXLocation); PrintToConsole(listOfPoints, "After our cool sorting!"); } Before sorting X = 1, Y = 0 X = 1, Y = 0 X = 2, Y = 0 X = 0, Y = 0 After our cool sorting! X = 0, Y = 0 X = 1, Y = 0 X = 1, Y = 0 X = 2, Y = 0 Press any key to continue...
  • 77. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static int SortByXLocation(PointClass point1, PointClass point2) { if (point1.XLocation > point2.XLocation) return +1; else if (point1.XLocation < point2.XLocation) return -1; else return 0; } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; PrintToConsole(listOfPoints, "Before sorting"); listOfPoints.Sort(SortByXLocation); PrintToConsole(listOfPoints, "After our cool sorting!"); } Before sorting X = 1, Y = 0 X = 1, Y = 0 X = 2, Y = 0 X = 0, Y = 0 After our cool sorting! X = 0, Y = 0 X = 1, Y = 0 X = 1, Y = 0 X = 2, Y = 0 Press any key to continue...
  • 78. More Collections Functionalities
  • 79. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(1, 0))); }
  • 80. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(1, 0))); } False Press any key to continue...
  • 81. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(1, 0))); } False Press any key to continue... Why?!
  • 82. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(1, 0))); } False Press any key to continue... Because it’s comparing References not Values
  • 83. More Collections Functionalities public static void PrintToConsole(List<PointClass> list, string Msg) { Console.WriteLine(Msg); foreach (var point in list) { Console.WriteLine(point.ToString()); } } public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(1, 0))); } False Press any key to continue... So, what to do to compare values?!
  • 84. More Collections Functionalities
  • 85. More Collections Functionalities public class PointClass : IEquatable<PointClass> { public float XLocation { set; get; } public float YLocation { set; get; } public PointClass(float xLocation, float yLocation) { this.XLocation = xLocation; this.YLocation = yLocation; } public override string ToString() { return String.Format("X = {0}, Y = {1}", this.XLocation, this.YLocation); } public bool Equals(PointClass other) { if ((this.XLocation == other.XLocation) && (this.YLocation == other.YLocation)) { return true; } else { return false; } } }
  • 86. More Collections Functionalities public class PointClass : IEquatable<PointClass> { public float XLocation { set; get; } public float YLocation { set; get; } public PointClass(float xLocation, float yLocation) { this.XLocation = xLocation; this.YLocation = yLocation; } public override string ToString() { return String.Format("X = {0}, Y = {1}", this.XLocation, this.YLocation); } public bool Equals(PointClass other) { if ((this.XLocation == other.XLocation) && (this.YLocation == other.YLocation)) { return true; } else { return false; } } }
  • 87. More Collections Functionalities public class PointClass : IEquatable<PointClass> { public float XLocation { set; get; } public float YLocation { set; get; } public PointClass(float xLocation, float yLocation) { this.XLocation = xLocation; this.YLocation = yLocation; } public override string ToString() { return String.Format("X = {0}, Y = {1}", this.XLocation, this.YLocation); } public bool Equals(PointClass other) { if ((this.XLocation == other.XLocation) && (this.YLocation == other.YLocation)) { return true; } else { return false; } } }
  • 88. More Collections Functionalities
  • 89. More Collections Functionalities public class PointClass : IEquatable<PointClass> { public float XLocation { set; get; } public float YLocation { set; get; } public PointClass(float xLocation, float yLocation) { this.XLocation = xLocation; this.YLocation = yLocation; } public override string ToString() { return String.Format("X = {0}, Y = {1}", this.XLocation, this.YLocation); } public bool Equals(PointClass other) { if ((this.XLocation == other.XLocation) && (this.YLocation == other.YLocation)) { return true; } else { return false; } } }
  • 90. More Collections Functionalities public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(0, 0))); }
  • 91. More Collections Functionalities public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(0, 0))); } True Press any key to continue...
  • 92. More Collections Functionalities public static void Main() { List<PointClass> listOfPoints = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 0), new PointClass(0, 0) }; Console.WriteLine(listOfPoints.Contains(new PointClass(0, 0))); } True Press any key to continue...
  • 93. “Cross” Collections
  • 94. “CROSS” Collections public class PointClass { public float XLocation { set; get; } public float YLocation { set; get; } public PointClass(float xLocation, float yLocation) { this.XLocation = xLocation; this.YLocation = yLocation; } public override string ToString() { return String.Format("X = {0}, Y = {1}", this.XLocation, this.YLocation); } }
  • 95. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); }
  • 96. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); }
  • 97. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); }
  • 98. “CROSS” Collections
  • 99. “CROSS” Collections
  • 100. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); foreach (PointClass point in dic["LabsLocations"]) { Console.WriteLine(point); } }
  • 101. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); foreach (PointClass point in dic["LabsLocations"]) { Console.WriteLine(point); } } X = 1, Y = 7 X = 10, Y = 2 Press any key to continue...
  • 102. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].ForEach(PrintMe); } private static void PrintMe(PointClass point) { Console.WriteLine(point); }
  • 103. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].ForEach(PrintMe); } private static void PrintMe(PointClass point) { Console.WriteLine(point); } X = 1, Y = 7 X = 10, Y = 2 Press any key to continue...
  • 104. “CROSS” Collections
  • 105. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].Add(new PointClass(8,8)); }
  • 106. “CROSS” Collections
  • 107. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].Add(new PointClass(8,8)); Console.WriteLine(dic["LabsLocations"][2]); }
  • 108. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].Add(new PointClass(8,8)); Console.WriteLine(dic["LabsLocations"][2]); }
  • 109. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].Add(new PointClass(8,8)); Console.WriteLine(dic["LabsLocations"][2]); } X = 8, Y = 8 Press any key to continue...
  • 110. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].Add(new PointClass(8,8)); Console.WriteLine(dic["LabsLocations"].Last()); }
  • 111. “CROSS” Collections public static void Main() { List<PointClass> listOfHalls = new List<PointClass>() { new PointClass(1, 0), new PointClass(1, 0), new PointClass(2, 3), }; List<PointClass> listOfLabs = new List<PointClass>() { new PointClass(1, 7), new PointClass(10, 2), }; Dictionary<string, List<PointClass>> dic = new Dictionary<string, List<PointClass>>(); dic.Add("HallsLocations", listOfHalls); dic.Add("LabsLocations", listOfLabs); dic["LabsLocations"].Add(new PointClass(8,8)); Console.WriteLine(dic["LabsLocations"].Last()); } X = 8, Y = 8 Press any key to continue...