SlideShare une entreprise Scribd logo
1  sur  24
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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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

Contenu connexe

Tendances

Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basicsH K
 
Template Haskell Tutorial
Template Haskell TutorialTemplate Haskell Tutorial
Template Haskell Tutorialkizzx2
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanWei-Yuan Chang
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arraysHassan Dar
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flowMohammed Sikander
 
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210Mahmoud Samir Fayed
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumarSujith Kumar
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2RajKumar Rampelli
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetSamuel Lampa
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to ProgrammingCindy Royal
 

Tendances (20)

Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Java script introducation & basics
Java script introducation & basicsJava script introducation & basics
Java script introducation & basics
 
Template Haskell Tutorial
Template Haskell TutorialTemplate Haskell Tutorial
Template Haskell Tutorial
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Python review2
Python review2Python review2
Python review2
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat Sheet
 
Python review2
Python review2Python review2
Python review2
 
Java arrays
Java   arraysJava   arrays
Java arrays
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Handout - Introduction to Programming
Handout - Introduction to ProgrammingHandout - Introduction to Programming
Handout - Introduction to Programming
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
1. python
1. python1. python
1. python
 

En vedette

En vedette (9)

Whats New In C# 3.0
Whats New In C# 3.0Whats New In C# 3.0
Whats New In C# 3.0
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
 
Secure mvc application saineshwar
Secure mvc application   saineshwarSecure mvc application   saineshwar
Secure mvc application saineshwar
 
Free MVC project to learn for beginners.
Free MVC project to learn for beginners.Free MVC project to learn for beginners.
Free MVC project to learn for beginners.
 
C# language
C# languageC# language
C# language
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
C# 3.5 Features
C# 3.5 FeaturesC# 3.5 Features
C# 3.5 Features
 
Introduction to .NET Core & ASP.NET Core MVC
Introduction to .NET Core & ASP.NET Core MVCIntroduction to .NET Core & ASP.NET Core MVC
Introduction to .NET Core & ASP.NET Core MVC
 
Introduction to C# 6.0 and 7.0
Introduction to C# 6.0 and 7.0Introduction to C# 6.0 and 7.0
Introduction to C# 6.0 and 7.0
 

Similaire à Csharp4 arrays and_tuples

Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................suchitrapoojari984
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arraysJayanthiM19
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.pptcoding9
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...ssuser6478a8
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useMukesh Tekwani
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 

Similaire à Csharp4 arrays and_tuples (20)

Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
 

Plus de Abed Bukhari

Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsAbed Bukhari
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_castsAbed Bukhari
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_typesAbed Bukhari
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritanceAbed Bukhari
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 

Plus de Abed Bukhari (6)

Csharp4 generics
Csharp4 genericsCsharp4 generics
Csharp4 generics
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_casts
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
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.
  • 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

Notes de l'éditeur

  1. // 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;} }; } } }
  2. // 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); } } }
  3. //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 { } }
  4. //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]; } } } }
  5. //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;); } } } }