SlideShare a Scribd company logo
1 of 122
Download to read offline
Mohammad Shaker
mohammadshaker.com
C# Programming Course
@ZGTRShaker
2011, 2012, 2013, 2014
C# Starter
L04 – Collections
Collections
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]);
}
}
}
}
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]);
}
}
}
}
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]);
}
}
}
}
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]);
}
}
}
}
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]);
}
}
}
}
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]);
}
}
}
}
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...
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; }
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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...
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);
}
}
}
}
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);
}
}
}
}
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...
Collections
• System.Collections.Generic
• System.Collections
Collections
• foreach danger
– NEVER EVER manipulate (insertion, deletion) a collection in a “foreach” statement
– Just iterate (and/or update if you want.)
– But Why?
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);
}
}
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);
}
}
Collections
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!
Collections
Collections
Collections
Collections
Collections
Collections
Collections
Collections
Collections
Collections
Collections
Collections
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 + ", ");
}
}
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 + ", ");
}
}
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 + ", ");
}
}
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));
}
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));
}
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));
}
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));
}
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));
}
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));
}
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));
}
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));
}
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}
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);
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);
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);
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);
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);
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);
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'.
Collections
Collections
Collections
Collections
• Find, FindAll
public List<T> FindAll(
Predicate<T> match
)
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;
}
}
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));
}
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;
}
}
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...
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: };
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
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);
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);
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();
}
}
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...
Play more with Collections
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);
}
}
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");
}
}
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");
}
}
More Collections Functionalities
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!");
}
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!");
}
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...
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...
More Collections Functionalities
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)));
}
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...
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?!
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
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?!
More Collections Functionalities
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;
}
}
}
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;
}
}
}
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;
}
}
}
More Collections Functionalities
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;
}
}
}
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)));
}
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...
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...
“Cross” Collections
“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);
}
}
“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);
}
“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);
}
“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);
}
“CROSS” Collections
“CROSS” Collections
“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);
}
}
“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...
“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);
}
“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...
“CROSS” Collections
“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));
}
“CROSS” Collections
“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]);
}
“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]);
}
“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...
“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());
}
“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...

More Related Content

What's hot

TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBtdc-globalcode
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulationIntro C# Book
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189Mahmoud Samir Fayed
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Nishan Barot
 
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202Mahmoud Samir Fayed
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
C# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesAbhishek Sur
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data TypesEelco Visser
 
Functional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedFunctional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedSusan Potter
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 

What's hot (20)

C# 7
C# 7C# 7
C# 7
 
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189
 
Google Dart
Google DartGoogle Dart
Google Dart
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202
 
Java practical
Java practicalJava practical
Java practical
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
C# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and Features
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data Types
 
Functional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedFunctional Algebra: Monoids Applied
Functional Algebra: Monoids Applied
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 

Similar to C# Collections Tutorial - How to Use Lists and Dictionaries

C++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsC++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsMohammad Shaker
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdfaravlitraders2012
 
String in .net
String in .netString in .net
String in .netLarry Nung
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummiesknutmork
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical FileFahad Shaikh
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++Jay Patel
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUadAccount
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88Mahmoud Samir Fayed
 

Similar to C# Collections Tutorial - How to Use Lists and Dictionaries (20)

C++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsC++ Windows Forms L07 - Collections
C++ Windows Forms L07 - Collections
 
C# p9
C# p9C# p9
C# p9
 
Collection
CollectionCollection
Collection
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
C# Generics
C# GenericsC# Generics
C# Generics
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
String in .net
String in .netString in .net
String in .net
 
Java generics
Java genericsJava generics
Java generics
 
Java Generics for Dummies
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
Dotnet 18
Dotnet 18Dotnet 18
Dotnet 18
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
Object calisthenics
Object calisthenicsObject calisthenics
Object calisthenics
 
Java and j2ee_lab-manual
Java and j2ee_lab-manualJava and j2ee_lab-manual
Java and j2ee_lab-manual
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 
greenDAO
greenDAOgreenDAO
greenDAO
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 

More from Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian GraduateMohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyMohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game DevelopmentMohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesMohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - ColorMohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - TypographyMohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingMohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and ThreadingMohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSMohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsMohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsMohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and GamingMohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / ParseMohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesMohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and AdaptersMohammad Shaker
 

More from Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Recently uploaded

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Français Patch Tuesday - Avril
Français Patch Tuesday - AvrilFrançais Patch Tuesday - Avril
Français Patch Tuesday - AvrilIvanti
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...amber724300
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 

Recently uploaded (20)

UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Français Patch Tuesday - Avril
Français Patch Tuesday - AvrilFrançais Patch Tuesday - Avril
Français Patch Tuesday - Avril
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
JET Technology Labs White Paper for Virtualized Security and Encryption Techn...
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
How Tech Giants Cut Corners to Harvest Data for A.I.
How Tech Giants Cut Corners to Harvest Data for A.I.How Tech Giants Cut Corners to Harvest Data for A.I.
How Tech Giants Cut Corners to Harvest Data for A.I.
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 

C# Collections Tutorial - How to Use Lists and Dictionaries

  • 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L04 – 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...
  • 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); } }
  • 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!
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 42. 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 + ", "); } }
  • 43. 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 + ", "); } }
  • 44. 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 + ", "); } }
  • 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)); }
  • 48. 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)); }
  • 49. 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)); }
  • 50. 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)); }
  • 51. 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)); }
  • 52. 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)); }
  • 53. 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}
  • 54.
  • 55. 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);
  • 56. 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);
  • 57. 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);
  • 58. 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);
  • 59. 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);
  • 60. 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);
  • 61. 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'.
  • 65. Collections • Find, FindAll public List<T> FindAll( Predicate<T> match )
  • 66. 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; } }
  • 67. 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)); }
  • 68. 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; } }
  • 69. 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...
  • 70. 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: };
  • 71. 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
  • 72. 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);
  • 73. 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);
  • 74. 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(); } }
  • 75. 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...
  • 76. Play more with Collections
  • 77. 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); } }
  • 78. 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"); } }
  • 79.
  • 80. 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"); } }
  • 81.
  • 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 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!"); }
  • 84. 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!"); }
  • 85. 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...
  • 86. 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...
  • 88.
  • 89.
  • 90. 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))); }
  • 91. 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...
  • 92. 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?!
  • 93. 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
  • 94. 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?!
  • 96. 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; } } }
  • 97. 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; } } }
  • 98. 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; } } }
  • 100. 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; } } }
  • 101. 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))); }
  • 102. 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...
  • 103. 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...
  • 105. “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); } }
  • 106. “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); }
  • 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); }
  • 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); }
  • 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); foreach (PointClass point in dic["LabsLocations"]) { Console.WriteLine(point); } }
  • 112. “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...
  • 113. “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); }
  • 114. “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...
  • 116. “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)); }
  • 118. “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]); }
  • 119. “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]); }
  • 120. “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...
  • 121. “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()); }
  • 122. “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...