Csharp4 arrays and_tuples

1,471 views

Published on

  • Be the first to comment

Csharp4 arrays and_tuples

  1. 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
  2. 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. 3. Simple Arrays <ul><li>array declaration: </li></ul><ul><li>int[] myArray; </li></ul><ul><li>array initialization: </li></ul><ul><li>myArray = new int[4]; </li></ul><ul><li>int[] myArray = new int[4]; </li></ul><ul><li>int[] myArray = new int[] {4, 7, 11, 2}; </li></ul><ul><li>int[] myArray = {4, 7, 11, 2}; </li></ul>
  4. 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. 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. 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. 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. 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. 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. 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. 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. 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. 13. sorting string[] names = { “ Zaid&quot;, “ Bashar&quot;, “ Hamza&quot;, “ Abed&quot; }; Array.Sort(names); foreach (var name in names) { Console.WriteLine(name); }
  14. 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. 15. arrays as Parameters static void DisplayPersons(Person[] persons) { }
  16. 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. 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. 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);
  19. 19. Enumerations YieldDemo/Program.cs YieldDemo/GameMoves.cs YieldDemo/MusicTitles.cs
  20. 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. 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. 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. 23. Structural Comparison Both arrays and tuples implement the interfaces IStructuralEquatable and IStructuralComparable . StructuralComparison/Person.cs StructuralComparison/Program.cs
  24. 24. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

×