c# starter l04-collections
TRANSCRIPT
![Page 1: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/1.jpg)
Mohammad Shakermohammadshaker.com
C# Programming Course@ZGTRShaker
2011, 2012, 2013, 2014
C# StarterL04 – Collections
![Page 2: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/2.jpg)
Collections
![Page 3: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/3.jpg)
Collectionsusing 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]);}
}}
}
![Page 4: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/4.jpg)
Collectionsusing 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]);}
}}
}
![Page 5: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/5.jpg)
Collectionsusing 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]);}
}}
}
![Page 6: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/6.jpg)
Collectionsusing 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]);}
}}
}
![Page 7: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/7.jpg)
Collectionsusing 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]);}
}}
}
![Page 8: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/8.jpg)
Collectionsusing 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]);}
}}
}
![Page 9: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/9.jpg)
Collectionsusing 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: 1MyInts: 2MyInts: 3Press any key to continue...
![Page 10: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/10.jpg)
Collections - Dictionaries
• Let’s have the following classnamespace 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; }
}}
}
![Page 11: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/11.jpg)
Collections
• And have the following mainnamespace 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);
}}
}}
![Page 12: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/12.jpg)
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);
}}
}}
![Page 13: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/13.jpg)
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);
}}
}}
![Page 14: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/14.jpg)
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 1Customer ID: 2, Name: Cust 2Customer ID: 3, Name: Cust 3Press any key to continue...
![Page 15: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/15.jpg)
Collectionsnamespace 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);
}}
}}
![Page 16: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/16.jpg)
Collectionsnamespace 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);
}}
}}
![Page 17: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/17.jpg)
Collectionsnamespace 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: Cust0Customer ID: 1, Name: Cust1Customer ID: 2, Name: Cust2
Customer ID: 0, Name: Cust0Customer ID: 1, Name: ZGTRCustomer ID: 2, Name: Cust2Press any key to continue...
![Page 18: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/18.jpg)
Collections
• System.Collections.Generic
• System.Collections
![Page 19: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/19.jpg)
Collections
• foreach danger
– NEVER EVER manipulate (insertion, deletion) a collection in a “foreach” statement
– Just iterate (and/or update if you want.)
– But Why?
![Page 20: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/20.jpg)
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);}
}
![Page 21: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/21.jpg)
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);}
}
![Page 22: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/22.jpg)
Collections
![Page 23: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/23.jpg)
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!
![Page 24: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/24.jpg)
Collections
![Page 25: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/25.jpg)
Collections
![Page 26: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/26.jpg)
Collections
![Page 27: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/27.jpg)
![Page 28: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/28.jpg)
![Page 29: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/29.jpg)
![Page 30: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/30.jpg)
![Page 31: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/31.jpg)
![Page 32: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/32.jpg)
![Page 33: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/33.jpg)
Collections
![Page 34: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/34.jpg)
Collections
![Page 35: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/35.jpg)
Collections
![Page 36: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/36.jpg)
Collections
![Page 37: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/37.jpg)
Collections
![Page 38: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/38.jpg)
Collections
![Page 39: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/39.jpg)
Collections
![Page 40: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/40.jpg)
Collections
![Page 41: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/41.jpg)
Collections
![Page 42: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/42.jpg)
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 + ", ");}
}
![Page 43: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/43.jpg)
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 + ", ");}
}
![Page 44: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/44.jpg)
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 + ", ");}
}
![Page 45: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/45.jpg)
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));}
![Page 46: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/46.jpg)
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));}
![Page 47: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/47.jpg)
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));}
![Page 48: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/48.jpg)
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));}
![Page 49: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/49.jpg)
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));}
![Page 50: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/50.jpg)
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));}
![Page 51: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/51.jpg)
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));}
![Page 52: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/52.jpg)
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));}
![Page 53: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/53.jpg)
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}
![Page 54: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/54.jpg)
![Page 55: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/55.jpg)
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);
![Page 56: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/56.jpg)
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);
![Page 57: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/57.jpg)
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);
![Page 58: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/58.jpg)
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);
![Page 59: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/59.jpg)
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);
![Page 60: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/60.jpg)
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);
![Page 61: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/61.jpg)
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'.
![Page 62: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/62.jpg)
Collections
![Page 63: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/63.jpg)
Collections
![Page 64: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/64.jpg)
Collections
![Page 65: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/65.jpg)
Collections
• Find, FindAll
public List<T> FindAll(Predicate<T> match
)
![Page 66: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/66.jpg)
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;}
}
![Page 67: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/67.jpg)
Collectionspublic 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));}
![Page 68: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/68.jpg)
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;}
}
![Page 69: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/69.jpg)
Collections
TrueForAll(EndsWithSaurus): False
Find(EndsWithSaurus): Dilophosaurus
FindLast(EndsWithSaurus): Dilophosaurus
FindAll(EndsWithSaurus):Dilophosaurus
1 elements removed by RemoveAll(EndsWithSaurus).
List now contains:VelociraptorDeinonychus
Exists(EndsWithSaurus): FalsePress any key to continue...
![Page 70: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/70.jpg)
Collections
1: public sealed class Employee2: {3: public long Id { get; set; }4: public string Name { get; set; }5: public double Salary { get; set; }6: }
1: // empty2: var noEmployeeList = new List<Employee>();3: 4: // many items5: 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: };
![Page 71: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/71.jpg)
Collections
1: public sealed class Employee2: {3: public long Id { get; set; }4: public string Name { get; set; }5: public double Salary { get; set; }6: }
1: // empty2: var noEmployeeList = new List<Employee>();3: 4: // many items5: 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
![Page 72: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/72.jpg)
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 matches5: // even though the enumerable itself is not empty6: var noMatch = employees.Last(e => e.Id == 20);
1: // returns default(Employee) -- null -- because list is empty2: var empty = noEmployeeList.LastOrDefault();3: 4: // returns default(Employee) -- null -- because no item matches predicate.5: var noMatch = employees.Last(e => e.Id == 20);
![Page 73: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/73.jpg)
Collections
• ElementAt()
1: 2: // returns the second employee (index == 1) which is Jane Doe, Id == 73: var janeDoe = employees.ElementAt(1);4: 5: // since there's not 30 employees, this one will throw exception6: 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);
![Page 74: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/74.jpg)
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();}
}
![Page 75: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/75.jpg)
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();}
}
myStackCount: 3Values: ! World Hello
Press any key to continue...
![Page 76: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/76.jpg)
Play more with Collections
![Page 77: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/77.jpg)
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);}
}
![Page 78: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/78.jpg)
More Collections Functionalitiespublic 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");
}}
![Page 79: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/79.jpg)
![Page 80: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/80.jpg)
More Collections Functionalitiespublic 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");
}}
![Page 81: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/81.jpg)
![Page 82: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/82.jpg)
More Collections Functionalities
![Page 83: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/83.jpg)
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;
elsereturn 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!");
}
![Page 84: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/84.jpg)
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;
elsereturn 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!");
}
![Page 85: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/85.jpg)
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;
elsereturn 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 sortingX = 1, Y = 0X = 1, Y = 0X = 2, Y = 0X = 0, Y = 0After our cool sorting!X = 0, Y = 0X = 1, Y = 0X = 1, Y = 0X = 2, Y = 0Press any key to continue...
![Page 86: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/86.jpg)
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;
elsereturn 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 sortingX = 1, Y = 0X = 1, Y = 0X = 2, Y = 0X = 0, Y = 0After our cool sorting!X = 0, Y = 0X = 1, Y = 0X = 1, Y = 0X = 2, Y = 0Press any key to continue...
![Page 87: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/87.jpg)
More Collections Functionalities
![Page 88: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/88.jpg)
![Page 89: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/89.jpg)
![Page 90: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/90.jpg)
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)));
}
![Page 91: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/91.jpg)
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)));
}
FalsePress any key to continue...
![Page 92: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/92.jpg)
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)));
}
FalsePress any key to continue...
Why?!
![Page 93: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/93.jpg)
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)));
}
FalsePress any key to continue...
Because it’s comparing
References not Values
![Page 94: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/94.jpg)
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)));
}
FalsePress any key to continue...
So, what to do to
compare values?!
![Page 95: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/95.jpg)
More Collections Functionalities
![Page 96: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/96.jpg)
More Collections Functionalitiespublic 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;}
}}
![Page 97: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/97.jpg)
More Collections Functionalitiespublic 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;}
}}
![Page 98: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/98.jpg)
More Collections Functionalitiespublic 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;}
}}
![Page 99: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/99.jpg)
More Collections Functionalities
![Page 100: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/100.jpg)
More Collections Functionalitiespublic 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;}
}}
![Page 101: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/101.jpg)
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)));
}
![Page 102: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/102.jpg)
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)));
}
TruePress any key to continue...
![Page 103: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/103.jpg)
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)));
}
TruePress any key to continue...
![Page 104: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/104.jpg)
“Cross” Collections
![Page 105: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/105.jpg)
“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);}
}
![Page 106: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/106.jpg)
“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);
}
![Page 107: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/107.jpg)
“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);
}
![Page 108: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/108.jpg)
“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);
}
![Page 109: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/109.jpg)
“CROSS” Collections
![Page 110: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/110.jpg)
“CROSS” Collections
![Page 111: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/111.jpg)
“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);}
}
![Page 112: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/112.jpg)
“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 = 7X = 10, Y = 2Press any key to continue...
![Page 113: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/113.jpg)
“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);}
![Page 114: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/114.jpg)
“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 = 7X = 10, Y = 2Press any key to continue...
![Page 115: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/115.jpg)
“CROSS” Collections
![Page 116: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/116.jpg)
“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));
}
![Page 117: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/117.jpg)
“CROSS” Collections
![Page 118: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/118.jpg)
“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]);
}
![Page 119: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/119.jpg)
“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]);
}
![Page 120: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/120.jpg)
“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 = 8Press any key to continue...
![Page 121: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/121.jpg)
“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());
}
![Page 122: C# Starter L04-Collections](https://reader034.vdocuments.net/reader034/viewer/2022052505/554ebcd1b4c905de468b4865/html5/thumbnails/122.jpg)
“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 = 8Press any key to continue...