SlideShare a Scribd company logo
Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
Arrays and Tuples Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? . Simple arrays . Multidimensional arrays . Jagged arrays . The Array class . Arrays as parameters . Enumerations . Tuples . Structural comparison
Simple Arrays array declaration: int[] myArray; array initialization: myArray = new int[4]; int[] myArray = new int[4]; int[] myArray = new int[] {4, 7, 11, 2}; int[] myArray = {4, 7, 11, 2};
accessing array elements int[] myArray = new int[] {4, 7, 11, 2}; int v1 = myArray[0]; // read first element int v2 = myArray[1]; // read second element myArray[3] = 44; // change fourth element //you can use the Length property that is used in this //for  for (int i = 0; i < myArray.Length; i++) { Console.WriteLine(myArray[i]); } statement: /you can also use the foreach statement: foreach (var val in myArray) { Console.WriteLine(val); }
using reference Types public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format(&quot;{0} {1}&quot;, FirstName, LastName); } } Person[] myPersons = new Person[2];
using reference Types cont. myPersons[0] = new Person { FirstName=“Ahmad&quot;, LastName=“Shawahne&quot; }; myPersons[1] = new Person { FirstName=“Abed&quot;, LastName=“Bukhari&quot; }; Person[] myPersons2 = { new Person { FirstName=“Hamza&quot;, LastName=“Mohammad&quot;}, new Person { FirstName=“Khaled&quot;, LastName=“Ahmad&quot;} };
multidimensional arrays int [,]  twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; // or int [,]  twodim = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
multidimensional arrays cont. By using two commas inside the brackets, you can declare a three - dimensional array: int [,,]  threedim = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }, { { 9, 10 }, { 11, 12 } } }; Console.WriteLine(threedim[0, 1, 1]);
jagged arrays int [][]  jagged = new int[3][]; jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 };
jagged arrays cont. for (int row = 0; row < jagged.Length; row++) { for (int element = 0; element < jagged[row].Length; element++) { Console.WriteLine(&quot; row: {0}, element: {1}, value: {2} &quot;, row ,  element ,  jagged[row][element] ); } } The outcome of the iteration displays the rows and every element within the rows: row: 0, element: 0, value: 1 row: 0, element: 1, value: 2 row: 1, element: 0, value: 3 row: 1, element: 1, value: 4 row: 1, element: 2, value: 5 row: 1, element: 3, value: 6 row: 1, element: 4, value: 7 row: 1, element: 5, value: 8 row: 2, element: 1, value: 9 row: 2, element: 2, value: 10 row: 2, element: 3, value: 11
Array Class Creating arrays: Array intArray1 = Array.CreateInstance(typeof(int), 5); for (int i = 0; i < 5; i++) { intArray1.SetValue(33, i); } for (int i = 0; i < 5; i++) { Console.WriteLine(intArray1.GetValue(i)); } // you can use casting to assing the array to another array int[] intArray2 = (int[])intArray1; Sample1/Program.cs
Copying arrays int[] intArray1 = {1, 2}; int[] intArray2 = (int[])intArray1.Clone(); Sample1/Program.cs Instead of using the Clone() method, you can use the Array.Copy() method, which creates a shallow copy as well.  But there’s one important difference with Clone() and Copy() :  Clone() creates a new array;  With Copy() you have to pass an existing array with the same rank and enough elements. If you need a deep copy of an array containing reference types, you have to iterate the array and create new objects.
sorting string[] names = { “ Zaid&quot;, “ Bashar&quot;, “ Hamza&quot;, “ Abed&quot; }; Array.Sort(names); foreach (var name in names) { Console.WriteLine(name); }
sorting string[] names = { “ Zaid&quot;, “ Bashar&quot;, “ Hamza&quot;, “ Abed&quot; }; Array.Sort(names); foreach (var name in names) { Console.WriteLine(name); } SortingSample/Person.cs SortingSample/Program.cs SortingSample/PersonComparer.cs SortingSample/Musician.cs
arrays as Parameters static void DisplayPersons(Person[] persons) { }
Array Covariance For example, you can declare a parameter of type object[] as shown and pass a Person[] to it: static void DisplayArray( object [] data) { //... } Array covariance is only possible with reference types, not with value types.
Arraysegment < T > static int SumOfSegments( ArraySegment<int>[]  segments) { int sum = 0; foreach (var segment in segments) { for (int i = segment .Offset ; i < segment .Offset  + segment .Count ; i++) { sum += segment.Array[i]; } } return sum; } ArraySegmentSample/Program.cs
Arraysegment < T > cont int[] ar1 = { 1, 4, 5, 11, 13, 18 }; int[] ar2 = { 3, 4, 5, 18, 21, 27, 33 }; var segments = new ArraySegment <int>[2] { new ArraySegment<int> (ar1, 0, 3), new ArraySegment<int> (ar2, 3, 3) }; var sum = SumOfSegments(segments);
Enumerations YieldDemo/Program.cs YieldDemo/GameMoves.cs YieldDemo/MusicTitles.cs
NET 4.0 : System.Tuple   A  tuple  is data structure which can contain different types of data coupled. This is what makes it different from a List or other generic types.
NET 4.0 : System.Tuple  cont  var squaresList =  Tuple.Create (1, 4, 9, 16, 25, 36, 49, 64); Console.WriteLine(&quot;1st item: {0}&quot;, squaresList.Item1); Console.WriteLine(&quot;4th item: {0}&quot;, squaresList.Item4); Console.WriteLine(&quot;8th item: {0}&quot;, squaresList.Rest);
NET 4.0 : System.Tuple  cont  var tupleWithMoreThan8Elements = System.Tuple.Create(&quot;is&quot;, 2.3, 4.0 f , new List { 'e', 't', 'h' }, “najah&quot;, new Stack(4), &quot;best&quot;, squaresList);  // we'll sort the list of chars in descending order  tupleWithMoreThan8Elements.Item4 .Sort();  tupleWithMoreThan8Elements.Item4 .Reverse();  Console.WriteLine(&quot;{ 0 } { 1 } { 2 } { 3 }&quot;,  tupleWithMoreThan8Elements.Item5 ,  tupleWithMoreThan8Elements.Item1 ,  string.Concat(tupleWithMoreThan8Elements.Item4) ,  tupleWithMoreThan8Elements.Item7 );  Console.WriteLine(&quot;Rest: {0}&quot;,  tupleWithMoreThan8Elements.Rest );  Console.WriteLine(&quot;Rest's 2nd element: {0}&quot;,  tupleWithMoreThan8Elements. Rest.Item1.Item2 );  Console.WriteLine(&quot;Rest's 5th element: {0}&quot;,tupleWithMoreThan8Elements .Rest.Item1.Item5 );
Structural Comparison Both arrays and tuples implement the interfaces  IStructuralEquatable  and  IStructuralComparable . StructuralComparison/Person.cs StructuralComparison/Program.cs
Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

More Related Content

PDF
PDF
Python dictionary : past, present, future
PDF
Python Workshop Part 2. LUG Maniapl
PDF
Array String - Web Programming
DOCX
What are arrays in java script
PPTX
Haste (Same Language, Multiple Platforms) and Tagless Final Style (Same Synta...
PPT
JavaScript Arrays
Python dictionary : past, present, future
Python Workshop Part 2. LUG Maniapl
Array String - Web Programming
What are arrays in java script
Haste (Same Language, Multiple Platforms) and Tagless Final Style (Same Synta...
JavaScript Arrays

What's hot (20)

PPTX
Java script arrays
PPTX
Datastructures in python
PDF
Java script introducation & basics
 
PDF
Template Haskell Tutorial
PDF
Python fundamentals - basic | WeiYuan
PPT
Javascript arrays
PPTX
Arrays basics
PDF
Python review2
PPTX
Java notes 1 - operators control-flow
PDF
The Ring programming language version 1.9 book - Part 39 of 210
PPT
Oop lecture7
PPTX
Python Datatypes by SujithKumar
PPTX
Learn python - for beginners - part-2
PDF
iRODS Rule Language Cheat Sheet
PPT
Python review2
PPTX
Java arrays
PDF
Python programming : Standard Input and Output
PDF
Handout - Introduction to Programming
PDF
Arrays and strings in c++
PDF
1. python
Java script arrays
Datastructures in python
Java script introducation & basics
 
Template Haskell Tutorial
Python fundamentals - basic | WeiYuan
Javascript arrays
Arrays basics
Python review2
Java notes 1 - operators control-flow
The Ring programming language version 1.9 book - Part 39 of 210
Oop lecture7
Python Datatypes by SujithKumar
Learn python - for beginners - part-2
iRODS Rule Language Cheat Sheet
Python review2
Java arrays
Python programming : Standard Input and Output
Handout - Introduction to Programming
Arrays and strings in c++
1. python
Ad

Viewers also liked (9)

PPT
Whats New In C# 3.0
PPT
Csharp4 delegates lambda_and_events
PDF
Secure mvc application saineshwar
PDF
Free MVC project to learn for beginners.
PPTX
C# language
PPT
Csharp4 basics
PPTX
PPTX
Introduction to .NET Core & ASP.NET Core MVC
PPTX
Introduction to C# 6.0 and 7.0
Whats New In C# 3.0
Csharp4 delegates lambda_and_events
Secure mvc application saineshwar
Free MVC project to learn for beginners.
C# language
Csharp4 basics
Introduction to .NET Core & ASP.NET Core MVC
Introduction to C# 6.0 and 7.0
Ad

Similar to Csharp4 arrays and_tuples (20)

DOC
Arrays In General
PPTX
arrays in c# including Classes handling arrays
PPTX
Arrays, Structures And Enums
PPTX
Arrays in .Net Programming Language.pptx
PPTX
7array in c#
PPT
Lecture 9
PDF
Learn C# Programming - Nullables & Arrays
PDF
Array and Collections in c#
PPT
07 Arrays
PPTX
Module 7 : Arrays
PPTX
Arrays.pptx
PDF
Intake 38 3
PPT
Csharp In Detail Part2
PPT
Visula C# Programming Lecture 5
PPTX
C# Lesson 3
PPTX
Visual Programing basic lectures 7.pptx
PPTX
PDF
C sharp chap6
PDF
LectureNotes-05-DSA
Arrays In General
arrays in c# including Classes handling arrays
Arrays, Structures And Enums
Arrays in .Net Programming Language.pptx
7array in c#
Lecture 9
Learn C# Programming - Nullables & Arrays
Array and Collections in c#
07 Arrays
Module 7 : Arrays
Arrays.pptx
Intake 38 3
Csharp In Detail Part2
Visula C# Programming Lecture 5
C# Lesson 3
Visual Programing basic lectures 7.pptx
C sharp chap6
LectureNotes-05-DSA

More from Abed Bukhari (6)

PPT
Csharp4 generics
PPT
Csharp4 strings and_regular_expressions
PPT
Csharp4 operators and_casts
PPT
Csharp4 objects and_types
PPT
Csharp4 inheritance
PPT
Whats new in_csharp4
Csharp4 generics
Csharp4 strings and_regular_expressions
Csharp4 operators and_casts
Csharp4 objects and_types
Csharp4 inheritance
Whats new in_csharp4

Csharp4 arrays and_tuples

  • 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
  • 2. Arrays and Tuples Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? . Simple arrays . Multidimensional arrays . Jagged arrays . The Array class . Arrays as parameters . Enumerations . Tuples . Structural comparison
  • 3. Simple Arrays array declaration: int[] myArray; array initialization: myArray = new int[4]; int[] myArray = new int[4]; int[] myArray = new int[] {4, 7, 11, 2}; int[] myArray = {4, 7, 11, 2};
  • 4. accessing array elements int[] myArray = new int[] {4, 7, 11, 2}; int v1 = myArray[0]; // read first element int v2 = myArray[1]; // read second element myArray[3] = 44; // change fourth element //you can use the Length property that is used in this //for for (int i = 0; i < myArray.Length; i++) { Console.WriteLine(myArray[i]); } statement: /you can also use the foreach statement: foreach (var val in myArray) { Console.WriteLine(val); }
  • 5. using reference Types public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format(&quot;{0} {1}&quot;, FirstName, LastName); } } Person[] myPersons = new Person[2];
  • 6. using reference Types cont. myPersons[0] = new Person { FirstName=“Ahmad&quot;, LastName=“Shawahne&quot; }; myPersons[1] = new Person { FirstName=“Abed&quot;, LastName=“Bukhari&quot; }; Person[] myPersons2 = { new Person { FirstName=“Hamza&quot;, LastName=“Mohammad&quot;}, new Person { FirstName=“Khaled&quot;, LastName=“Ahmad&quot;} };
  • 7. multidimensional arrays int [,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; // or int [,] twodim = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
  • 8. multidimensional arrays cont. By using two commas inside the brackets, you can declare a three - dimensional array: int [,,] threedim = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }, { { 9, 10 }, { 11, 12 } } }; Console.WriteLine(threedim[0, 1, 1]);
  • 9. jagged arrays int [][] jagged = new int[3][]; jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 };
  • 10. jagged arrays cont. for (int row = 0; row < jagged.Length; row++) { for (int element = 0; element < jagged[row].Length; element++) { Console.WriteLine(&quot; row: {0}, element: {1}, value: {2} &quot;, row , element , jagged[row][element] ); } } The outcome of the iteration displays the rows and every element within the rows: row: 0, element: 0, value: 1 row: 0, element: 1, value: 2 row: 1, element: 0, value: 3 row: 1, element: 1, value: 4 row: 1, element: 2, value: 5 row: 1, element: 3, value: 6 row: 1, element: 4, value: 7 row: 1, element: 5, value: 8 row: 2, element: 1, value: 9 row: 2, element: 2, value: 10 row: 2, element: 3, value: 11
  • 11. Array Class Creating arrays: Array intArray1 = Array.CreateInstance(typeof(int), 5); for (int i = 0; i < 5; i++) { intArray1.SetValue(33, i); } for (int i = 0; i < 5; i++) { Console.WriteLine(intArray1.GetValue(i)); } // you can use casting to assing the array to another array int[] intArray2 = (int[])intArray1; Sample1/Program.cs
  • 12. Copying arrays int[] intArray1 = {1, 2}; int[] intArray2 = (int[])intArray1.Clone(); Sample1/Program.cs Instead of using the Clone() method, you can use the Array.Copy() method, which creates a shallow copy as well. But there’s one important difference with Clone() and Copy() : Clone() creates a new array; With Copy() you have to pass an existing array with the same rank and enough elements. If you need a deep copy of an array containing reference types, you have to iterate the array and create new objects.
  • 13. sorting string[] names = { “ Zaid&quot;, “ Bashar&quot;, “ Hamza&quot;, “ Abed&quot; }; Array.Sort(names); foreach (var name in names) { Console.WriteLine(name); }
  • 14. sorting string[] names = { “ Zaid&quot;, “ Bashar&quot;, “ Hamza&quot;, “ Abed&quot; }; Array.Sort(names); foreach (var name in names) { Console.WriteLine(name); } SortingSample/Person.cs SortingSample/Program.cs SortingSample/PersonComparer.cs SortingSample/Musician.cs
  • 15. arrays as Parameters static void DisplayPersons(Person[] persons) { }
  • 16. Array Covariance For example, you can declare a parameter of type object[] as shown and pass a Person[] to it: static void DisplayArray( object [] data) { //... } Array covariance is only possible with reference types, not with value types.
  • 17. Arraysegment < T > static int SumOfSegments( ArraySegment<int>[] segments) { int sum = 0; foreach (var segment in segments) { for (int i = segment .Offset ; i < segment .Offset + segment .Count ; i++) { sum += segment.Array[i]; } } return sum; } ArraySegmentSample/Program.cs
  • 18. Arraysegment < T > cont int[] ar1 = { 1, 4, 5, 11, 13, 18 }; int[] ar2 = { 3, 4, 5, 18, 21, 27, 33 }; var segments = new ArraySegment <int>[2] { new ArraySegment<int> (ar1, 0, 3), new ArraySegment<int> (ar2, 3, 3) }; var sum = SumOfSegments(segments);
  • 20. NET 4.0 : System.Tuple A tuple is data structure which can contain different types of data coupled. This is what makes it different from a List or other generic types.
  • 21. NET 4.0 : System.Tuple cont var squaresList = Tuple.Create (1, 4, 9, 16, 25, 36, 49, 64); Console.WriteLine(&quot;1st item: {0}&quot;, squaresList.Item1); Console.WriteLine(&quot;4th item: {0}&quot;, squaresList.Item4); Console.WriteLine(&quot;8th item: {0}&quot;, squaresList.Rest);
  • 22. NET 4.0 : System.Tuple cont var tupleWithMoreThan8Elements = System.Tuple.Create(&quot;is&quot;, 2.3, 4.0 f , new List { 'e', 't', 'h' }, “najah&quot;, new Stack(4), &quot;best&quot;, squaresList); // we'll sort the list of chars in descending order tupleWithMoreThan8Elements.Item4 .Sort(); tupleWithMoreThan8Elements.Item4 .Reverse(); Console.WriteLine(&quot;{ 0 } { 1 } { 2 } { 3 }&quot;, tupleWithMoreThan8Elements.Item5 , tupleWithMoreThan8Elements.Item1 , string.Concat(tupleWithMoreThan8Elements.Item4) , tupleWithMoreThan8Elements.Item7 ); Console.WriteLine(&quot;Rest: {0}&quot;, tupleWithMoreThan8Elements.Rest ); Console.WriteLine(&quot;Rest's 2nd element: {0}&quot;, tupleWithMoreThan8Elements. Rest.Item1.Item2 ); Console.WriteLine(&quot;Rest's 5th element: {0}&quot;,tupleWithMoreThan8Elements .Rest.Item1.Item5 );
  • 23. Structural Comparison Both arrays and tuples implement the interfaces IStructuralEquatable and IStructuralComparable . StructuralComparison/Person.cs StructuralComparison/Program.cs
  • 24. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Editor's Notes

  • #12: // Sample1/Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { class Program { static void Main() { SimpleArrays(); TwoDim(); ThreeDim(); Jagged(); ArrayClass(); CopyArrays(); } static void CopyArrays() { Person[] beatles = { new Person { FirstName=&amp;quot;John&amp;quot;, LastName=&amp;quot;Lennon&amp;quot; }, new Person { FirstName=&amp;quot;Paul&amp;quot;, LastName=&amp;quot;McCartney&amp;quot; } }; Person[] beatlesClone = (Person[])beatles.Clone(); } static void ArrayClass() { Array intArray1 = Array.CreateInstance(typeof(int), 5); for (int i = 0; i &lt; 5; i++) { intArray1.SetValue(33, i); } for (int i = 0; i &lt; 5; i++) { Console.WriteLine(intArray1.GetValue(i)); } int[] lengths = { 2, 3 }; int[] lowerBounds = { 1, 10 }; Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds); racers.SetValue(new Person { FirstName = &amp;quot;Alain&amp;quot;, LastName = &amp;quot;Prost&amp;quot; }, 1, 10); racers.SetValue(new Person { FirstName = &amp;quot;Emerson&amp;quot;, LastName = &amp;quot;Fittipaldi&amp;quot; }, 1, 11); racers.SetValue(new Person { FirstName = &amp;quot;Ayrton&amp;quot;, LastName = &amp;quot;Senna&amp;quot; }, 1, 12); racers.SetValue(new Person { FirstName = &amp;quot;Ralf&amp;quot;, LastName = &amp;quot;Schumacher&amp;quot; }, 2, 10); racers.SetValue(new Person { FirstName = &amp;quot;Fernando&amp;quot;, LastName = &amp;quot;Alonso&amp;quot; }, 2, 11); racers.SetValue(new Person { FirstName = &amp;quot;Jenson&amp;quot;, LastName = &amp;quot;Button&amp;quot; }, 2, 12); Person[,] racers2 = (Person[,])racers; Person first = racers2[1, 10]; Person last = racers2[2, 12]; } static void Jagged() { int[][] jagged = new int[3][]; jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 }; for (int row = 0; row &lt; jagged.Length; row++) { for (int element = 0; element &lt; jagged[row].Length; element++) { Console.WriteLine( &amp;quot;row: {0}, element: {1}, value: {2}&amp;quot;, row, element, jagged[row][element]); } } } static void ThreeDim() { int[, ,] threedim = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }, { { 9, 10 }, { 11, 12 } } }; Console.WriteLine(threedim[0, 1, 1]); } static void TwoDim() { int[,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; } static void SimpleArrays() { Person[] myPersons = new Person[2]; myPersons[0] = new Person { FirstName = &amp;quot;Ayrton&amp;quot;, LastName = &amp;quot;Senna&amp;quot; }; myPersons[1] = new Person { FirstName = &amp;quot;Michael&amp;quot;, LastName = &amp;quot;Schumacher&amp;quot; }; Person[] myPersons2 = { new Person { FirstName=&amp;quot;Ayrton&amp;quot;, LastName=&amp;quot;Senna&amp;quot;}, new Person { FirstName=&amp;quot;Michael&amp;quot;, LastName=&amp;quot;Schumacher&amp;quot;} }; } } }
  • #13: // Sample1/Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { class Program { static void Main() { SimpleArrays(); TwoDim(); ThreeDim(); Jagged(); ArrayClass(); CopyArrays(); } static void CopyArrays() { Person[] beatles = { new Person { FirstName=&amp;quot;John&amp;quot;, LastName=&amp;quot;Lennon&amp;quot; }, new Person { FirstName=&amp;quot;Paul&amp;quot;, LastName=&amp;quot;McCartney&amp;quot; } }; Person[] beatlesClone = (Person[])beatles.Clone(); } static void ArrayClass() { Array intArray1 = Array.CreateInstance(typeof(int), 5); for (int i = 0; i &lt; 5; i++) { intArray1.SetValue(33, i); } for (int i = 0; i &lt; 5; i++) { Console.WriteLine(intArray1.GetValue(i)); } int[] lengths = { 2, 3 }; int[] lowerBounds = { 1, 10 }; Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds); racers.SetValue(new Person { FirstName = &amp;quot;Alain&amp;quot;, LastName = &amp;quot;Prost&amp;quot; }, 1, 10); racers.SetValue(new Person { FirstName = &amp;quot;Emerson&amp;quot;, LastName = &amp;quot;Fittipaldi&amp;quot; }, 1, 11); racers.SetValue(new Person { FirstName = &amp;quot;Ayrton&amp;quot;, LastName = &amp;quot;Senna&amp;quot; }, 1, 12); racers.SetValue(new Person { FirstName = &amp;quot;Ralf&amp;quot;, LastName = &amp;quot;Schumacher&amp;quot; }, 2, 10); racers.SetValue(new Person { FirstName = &amp;quot;Fernando&amp;quot;, LastName = &amp;quot;Alonso&amp;quot; }, 2, 11); racers.SetValue(new Person { FirstName = &amp;quot;Jenson&amp;quot;, LastName = &amp;quot;Button&amp;quot; }, 2, 12); Person[,] racers2 = (Person[,])racers; Person first = racers2[1, 10]; Person last = racers2[2, 12]; } static void Jagged() { int[][] jagged = new int[3][]; jagged[0] = new int[2] { 1, 2 }; jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 }; jagged[2] = new int[3] { 9, 10, 11 }; for (int row = 0; row &lt; jagged.Length; row++) { for (int element = 0; element &lt; jagged[row].Length; element++) { Console.WriteLine( &amp;quot;row: {0}, element: {1}, value: {2}&amp;quot;, row, element, jagged[row][element]); } } } static void ThreeDim() { int[, ,] threedim = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }, { { 9, 10 }, { 11, 12 } } }; Console.WriteLine(threedim[0, 1, 1]); } static void TwoDim() { int[,] twodim = new int[3, 3]; twodim[0, 0] = 1; twodim[0, 1] = 2; twodim[0, 2] = 3; twodim[1, 0] = 4; twodim[1, 1] = 5; twodim[1, 2] = 6; twodim[2, 0] = 7; twodim[2, 1] = 8; twodim[2, 2] = 9; } static void SimpleArrays() { Person[] myPersons = new Person[2]; myPersons[0] = new Person { FirstName = &amp;quot;Ayrton&amp;quot;, LastName = &amp;quot;Senna&amp;quot; }; myPersons[1] = new Person { FirstName = &amp;quot;Michael&amp;quot;, LastName = &amp;quot;Schumacher&amp;quot; }; Person[] myPersons2 = { new Person { FirstName=&amp;quot;Ayrton&amp;quot;, LastName=&amp;quot;Senna&amp;quot;}, new Person { FirstName=&amp;quot;Michael&amp;quot;, LastName=&amp;quot;Schumacher&amp;quot;} }; } } } //sample1/person.cs using System; namespace Najah.ILoveCsharp.Arrays { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format(&amp;quot;{0} {1}&amp;quot;, FirstName, LastName); } } }
  • #15: //SortingSample/Person.cs using System; namespace Najah.ILoveCsharp.Arrays { public class Person : IComparable&lt;Person&gt; { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format(&amp;quot;{0} {1}&amp;quot;, FirstName, LastName); } public int CompareTo(Person other) { if (other == null) throw new ArgumentNullException(&amp;quot;other&amp;quot;); int result = this.LastName.CompareTo(other.LastName); if (result == 0) { result = this.FirstName.CompareTo(other.FirstName); } return result; } } } //SortingSample/Program.cs using System; namespace Najah.ILoveCsharp.Arrays { class Program { static void Main() { SortNames(); Person[] persons = GetPersons(); SortPersons(persons); Console.WriteLine(); SortUsingPersonComparer(persons); Covariance(persons); } static void Covariance(object[] objects) { } static void SortUsingPersonComparer(Person[] persons) { Array.Sort(persons, new PersonComparer(PersonCompareType.FirstName)); foreach (Person p in persons) { Console.WriteLine(p); } } static Person[] GetPersons() { return new Person[] { new Person { FirstName=&amp;quot;Damon&amp;quot;, LastName=&amp;quot;Hill&amp;quot; }, new Person { FirstName=&amp;quot;Niki&amp;quot;, LastName=&amp;quot;Lauda&amp;quot; }, new Person { FirstName=&amp;quot;Ayrton&amp;quot;, LastName=&amp;quot;Senna&amp;quot; }, new Person { FirstName=&amp;quot;Graham&amp;quot;, LastName=&amp;quot;Hill&amp;quot; } }; } static void SortPersons(Person[] persons) { Array.Sort(persons); foreach (Person p in persons) { Console.WriteLine(p); } } static void SortNames() { string[] names = { &amp;quot;Christina Aguilera&amp;quot;, &amp;quot;Shakira&amp;quot;, &amp;quot;Beyonce&amp;quot;, &amp;quot;Gwen Stefani&amp;quot; }; Array.Sort(names); foreach (string name in names) { Console.WriteLine(name); } } } } //SortingSample/PersonComparer.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { public enum PersonCompareType { FirstName, LastName } public class PersonComparer : IComparer&lt;Person&gt; { private PersonCompareType compareType; public PersonComparer(PersonCompareType compareType) { this.compareType = compareType; } #region IComparer&lt;Person&gt; Members public int Compare(Person x, Person y) { if (x == null) throw new ArgumentNullException(&amp;quot;x&amp;quot;); if (y == null) throw new ArgumentNullException(&amp;quot;y&amp;quot;); switch (compareType) { case PersonCompareType.FirstName: return x.FirstName.CompareTo(y.FirstName); case PersonCompareType.LastName: return x.LastName.CompareTo(y.LastName); default: throw new ArgumentException( &amp;quot;unexpected compare type&amp;quot;); } } #endregion } } //SortingSample/Musician.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { public class Musician : Person { } }
  • #20: //YieldDemo/Program.cs using System; using System.Collections; using System.Collections.Generic; namespace Najah.ILoveCsharp.Arrays { public class HelloCollection { public IEnumerator&lt;string&gt; GetEnumerator() { yield return &amp;quot;Hello&amp;quot;; yield return &amp;quot;World&amp;quot;; } } class Program { static void Main() { HelloWorld(); MusicTitles(); var game = new GameMoves(); IEnumerator enumerator = game.Cross(); while (enumerator.MoveNext()) { enumerator = enumerator.Current as IEnumerator; } } static void MusicTitles() { var titles = new MusicTitles(); foreach (var title in titles) { Console.WriteLine(title); } Console.WriteLine(); Console.WriteLine(&amp;quot;reverse&amp;quot;); foreach (var title in titles.Reverse()) { Console.WriteLine(title); } Console.WriteLine(); Console.WriteLine(&amp;quot;subset&amp;quot;); foreach (var title in titles.Subset(2, 2)) { Console.WriteLine(title); } } static void HelloWorld() { var helloCollection = new HelloCollection(); foreach (string s in helloCollection) { Console.WriteLine(s); } } } } //YieldDemo/GameMoves.cs using System; using System.Collections; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { public class GameMoves { private IEnumerator cross; private IEnumerator circle; public GameMoves() { cross = Cross(); circle = Circle(); } private int move = 0; const int MaxMoves = 9; public IEnumerator Cross() { while (true) { Console.WriteLine(&amp;quot;Cross, move {0}&amp;quot;, move); if (++move &gt;= MaxMoves) yield break; yield return circle; } } public IEnumerator Circle() { while (true) { Console.WriteLine(&amp;quot;Circle, move {0}&amp;quot;, move); if (++move &gt;= MaxMoves) yield break; yield return cross; } } } } //YieldDemo/MusicTitles.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { public class MusicTitles { string[] names = { &amp;quot;Tubular Bells&amp;quot;, &amp;quot;Hergest Ridge&amp;quot;, &amp;quot;Ommadawn&amp;quot;, &amp;quot;Platinum&amp;quot; }; public IEnumerator&lt;string&gt; GetEnumerator() { for (int i = 0; i &lt; 4; i++) { yield return names[i]; } } public IEnumerable&lt;string&gt; Reverse() { for (int i = 3; i &gt;= 0; i--) { yield return names[i]; } } public IEnumerable&lt;string&gt; Subset(int index, int length) { for (int i = index; i &lt; index + length; i++) { yield return names[i]; } } } }
  • #24: //StructuralComparison/Person.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { public class Person : IEquatable&lt;Person&gt; { public int Id { get; private set; } public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format(&amp;quot;{0}, {1} {2}&amp;quot;, Id, FirstName, LastName); } //public override bool Equals(object obj) //{ // throw new Exception(&amp;quot;xx&amp;quot;); // if (obj == null) throw new ArgumentNullException(&amp;quot;obj&amp;quot;); // return Equals(obj as Person); //} //public override int GetHashCode() //{ // return Id.GetHashCode(); //} #region IEquatable&lt;Person&gt; Members public bool Equals(Person other) { if (other == null) throw new ArgumentNullException(&amp;quot;other&amp;quot;); return this.FirstName == other.FirstName &amp;&amp; this.LastName == other.LastName; } #endregion } } //StructuralComparison/Program.cs using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace Najah.ILoveCsharp.Arrays { class TupleComparer : IEqualityComparer { #region IEqualityComparer Members public new bool Equals(object x, object y) { bool result = x.Equals(y); return result; } public int GetHashCode(object obj) { return obj.GetHashCode(); } #endregion } class Program { static void Main() { var janet = new Person { FirstName = &amp;quot;Janet&amp;quot;, LastName = &amp;quot;Jackson&amp;quot; }; Person[] persons1 = { new Person { FirstName = &amp;quot;Michael&amp;quot;, LastName = &amp;quot;Jackson&amp;quot; }, janet }; Person[] persons2 = { new Person { FirstName = &amp;quot;Michael&amp;quot;, LastName = &amp;quot;Jackson&amp;quot; }, janet }; if (persons1 != persons2) Console.WriteLine(&amp;quot;not the same reference&amp;quot;); if (!persons1.Equals(persons2)) Console.WriteLine(&amp;quot;equals returns false - not the same reference&amp;quot;); if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer&lt;Person&gt;.Default)) { Console.WriteLine(&amp;quot;the same content&amp;quot;); } var t1 = Tuple.Create&lt;int, string&gt;(1, &amp;quot;Stephanie&amp;quot;); var t2 = Tuple.Create&lt;int, string&gt;(1, &amp;quot;Stephanie&amp;quot;); if (t1 != t2) Console.WriteLine(&amp;quot;not the same reference to the tuple&amp;quot;); if (t1.Equals(t2)) Console.WriteLine(&amp;quot;equals returns true&amp;quot;); TupleComparer tc = new TupleComparer(); if ((t1 as IStructuralEquatable).Equals(t2, tc)) { Console.WriteLine(&amp;quot;yes, using TubpleComparer&amp;quot;); } } } }