Arrays<br />❑ Simple arrays<br />❑ Multidimensional arrays<br />❑ Jagged arrays<br />❑ The Array class<br />❑ Interfaces f...
Arrays<br />If you need to work with multiple objects of the same type, you can use collections and arrays.<br />Simple Ar...
Array Declaration & Array Initialization<br />The array cannot be resized after the size was specified without copying all...
Rajpat Systems<br />
Accessing Array Elements<br />After an array is declared and initialized, you can access the array elements using an index...
If you don ’ t know the number of elements in the array, you can use the Length property<br />	for (int i = 0; i < myArray...
Using Reference Types<br />In addition to being able to declare arrays of predefined types, you can also declare arrays of...
Declaring an array of two Person elements is similar to declaring an array of int<br />	Person[] myPersons = new Person[2]...
Rajpat Systems<br />
Multidimensional Arrays<br />You cannot change the rank after declaring an array.<br />int[,] twodim = new int[3, 3];<br /...
Using an array initializer<br />When using an array initializer, you must initialize every element of the array. It is not...
Rajpat Systems<br />
Jagged Arrays<br />A jagged array is more flexible in sizing the array.<br />With a jagged array every row can have a diff...
for (int row = 0; row < jagged.Length; row++)<br />{<br />      for (int element = 0; element < jagged[row].Length; elemen...
Array Class<br />Properties<br />Rajpat Systems<br />
Creating Arrays<br />The Array class is abstract, so you cannot create an array by using a constructor.<br />Rajpat System...
Rajpat Systems<br />
Copying Arrays<br />Because arrays are reference types, assigning an array variable to another one just gives you two vari...
Rajpat Systems<br />
If the array contains reference types, only the references are copied<br />Rajpat Systems<br />
Sorting<br />The Array class implements a bubble - sort for sorting the elements in the array.<br />The Sort() method requ...
Rajpat Systems<br />
If you are using custom classes with the array, you must implement the interface IComparable .<br />This interface defines...
Rajpat Systems<br />
Array and Collection Interfaces<br />The Array class implements the interfaces IEnumerable , ICollection , and IList for a...
IEnumerable<br />IEnumerable is an interface that is used by the foreach statement to iterate through the array<br />Rajpa...
ICollection<br />The interface ICollection derives from the interface IEnumerable and has additional properties and method...
Rajpat Systems<br />
IList<br />The IList interface derives from the interface ICollection and defines additional properties and methods.<br />...
Rajpat Systems<br />
IEnumerator Interface<br />The foreach statement uses the methods and properties of the IEnumerator interface to iterate a...
foreach Statement<br />The C# foreach statement is not resolved to a foreach statement in the IL code. Instead, the C# com...
Rajpat Systems<br />
Upcoming SlideShare
Loading in...5
×

Array in C# 3.5

3,096

Published on

Rajpat Systems Training Part

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,096
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
55
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Array in C# 3.5

  1. 1. Arrays<br />❑ Simple arrays<br />❑ Multidimensional arrays<br />❑ Jagged arrays<br />❑ The Array class<br />❑ Interfaces for arrays<br />❑ Enumerations<br />Rajpat Systems<br />
  2. 2. Arrays<br />If you need to work with multiple objects of the same type, you can use collections and arrays.<br />Simple Arrays<br />If you need to use multiple objects of the same type, you can use an array. An array is a data structure that contains a number of elements of the same type.<br />Rajpat Systems<br />
  3. 3. Array Declaration & Array Initialization<br />The array cannot be resized after the size was specified without copying all elements.<br />int[] myArray;<br />myArray = new int[4];<br />Using an array initializer<br />int[] myArray = new int[4] {4, 7, 11, 2};<br />int[] myArray = new int[] {4, 7, 11, 2};<br />int[] myArray = {4, 7, 11, 2};<br />Rajpat Systems<br />
  4. 4. Rajpat Systems<br />
  5. 5. Accessing Array Elements<br />After an array is declared and initialized, you can access the array elements using an indexer. Arrays only support indexers that have integer parameters.<br />With custom classes, you can also create indexers that support other types.<br />int[] myArray = new int[] {4, 7, 11, 2};<br />int v1 = myArray[0]; // read first element<br />int v2 = myArray[1]; // read second element<br />myArray[3] = 44; // change fourth element<br />If you use a wrong indexer value where no element exists, an exception of type IndexOutOfRangeExceptionis thrown.<br />Rajpat Systems<br />
  6. 6. If you don ’ t know the number of elements in the array, you can use the Length property<br /> for (int i = 0; i < myArray.Length; i++)<br /> {<br />Console.WriteLine(myArray[i]);<br /> }<br />Rajpat Systems<br />
  7. 7. Using Reference Types<br />In addition to being able to declare arrays of predefined types, you can also declare arrays of custom types.<br />public class Person<br />{<br /> public Person()<br /> {<br /> }<br /> public Person(string firstName, string lastName)<br /> {<br />this.FirstName = firstName;<br />this.LastName = lastName;<br /> }<br /> public string FirstName { get; set; }<br /> public string LastName { get; set; }<br /> public override string ToString()<br /> {<br /> return String.Format(“{0} {1}”, FirstName, LastName);<br /> }<br />}<br />Rajpat Systems<br />
  8. 8. Declaring an array of two Person elements is similar to declaring an array of int<br /> Person[] myPersons = new Person[2];<br />If the elements in the array are reference types, memory must be allocated for every array element.<br />In the array where no memory was allocated, a NullReferenceException is thrown.<br />myPersons[0] = new Person(“Ayrton”, “Senna”);<br />myPersons[1] = new Person(“Michael”, “Schumacher”);<br />Person[] myPersons = { new Person(“Ayrton”, “Senna”),<br /> new Person(“Michael”,“Schumacher”) };<br />Rajpat Systems<br />
  9. 9. Rajpat Systems<br />
  10. 10. Multidimensional Arrays<br />You cannot change the rank after declaring an array.<br />int[,] twodim = new int[3, 3];<br />twodim[0, 0] = 1;<br />twodim[0, 1] = 2;<br />twodim[0, 2] = 3;<br />twodim[1, 0] = 4;<br />twodim[1, 1] = 5;<br />twodim[1, 2] = 6;<br />twodim[2, 0] = 7;<br />twodim[2, 1] = 8;<br />twodim[2, 2] = 9;<br />Rajpat Systems<br />
  11. 11. Using an array initializer<br />When using an array initializer, you must initialize every element of the array. It is not possible to leave the initialization for some values.<br />Rajpat Systems<br />
  12. 12. Rajpat Systems<br />
  13. 13. Jagged Arrays<br />A jagged array is more flexible in sizing the array.<br />With a jagged array every row can have a different size.<br />int[][] jagged = new int[3][];<br />jagged[0] = new int[2] { 1, 2 };<br />jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };<br />jagged[2] = new int[3] { 9, 10, 11 };<br />Rajpat Systems<br />
  14. 14. for (int row = 0; row < jagged.Length; row++)<br />{<br /> for (int element = 0; element < jagged[row].Length; element++)<br /> {<br />Console.WriteLine(“row: {0}, element: {1}, value: {2}”,<br /> row, element, jagged[row][element]<br /> );<br /> }<br />}<br />Rajpat Systems<br />
  15. 15. Array Class<br />Properties<br />Rajpat Systems<br />
  16. 16. Creating Arrays<br />The Array class is abstract, so you cannot create an array by using a constructor.<br />Rajpat Systems<br />
  17. 17. Rajpat Systems<br />
  18. 18. Copying Arrays<br />Because arrays are reference types, assigning an array variable to another one just gives you two variables referencing the same array.<br />For copying arrays, the array implements the interface ICloneable . <br />The Clone() method that is defined with this interface creates a shallow copy of the array.<br />If the elements of the array are value types, all values are copied.<br />Rajpat Systems<br />
  19. 19. Rajpat Systems<br />
  20. 20. If the array contains reference types, only the references are copied<br />Rajpat Systems<br />
  21. 21. Sorting<br />The Array class implements a bubble - sort for sorting the elements in the array.<br />The Sort() method requires the interface IComparable to be implemented by the elements in the array.<br />Simple types such as System.String and System.Int32 implement IComparable , so you can sort elements containing these types.<br />Rajpat Systems<br />
  22. 22. Rajpat Systems<br />
  23. 23. If you are using custom classes with the array, you must implement the interface IComparable .<br />This interface defines just one method, CompareTo().<br />Rajpat Systems<br />
  24. 24. Rajpat Systems<br />
  25. 25. Array and Collection Interfaces<br />The Array class implements the interfaces IEnumerable , ICollection , and IList for accessing and enumerating the elements of the array.<br />Because with a custom array a class is created that derives from the abstract class Array , you can use the methods and properties of the implemented interfaces with an array variable.<br />Rajpat Systems<br />
  26. 26. IEnumerable<br />IEnumerable is an interface that is used by the foreach statement to iterate through the array<br />Rajpat Systems<br />
  27. 27. ICollection<br />The interface ICollection derives from the interface IEnumerable and has additional properties and methods.<br />This interface is mainly used to get the number of elements in a collection and for synchronization.<br />Rajpat Systems<br />
  28. 28. Rajpat Systems<br />
  29. 29. IList<br />The IList interface derives from the interface ICollection and defines additional properties and methods.<br />The major reason why the Array class implements the IList interface is that the IList interface defines the Item property for accessing the elements using an indexer.<br />Many of the other IList members are implemented by the Array class by throwing a NotSupportedExceptio n , because these do not apply to arrays.<br />Rajpat Systems<br />
  30. 30. Rajpat Systems<br />
  31. 31. IEnumerator Interface<br />The foreach statement uses the methods and properties of the IEnumerator interface to iterate all elements in a collection.<br />Rajpat Systems<br />
  32. 32. foreach Statement<br />The C# foreach statement is not resolved to a foreach statement in the IL code. Instead, the C# compiler converts the foreach statement to methods and properties of the IEnumerable interface.<br />Rajpat Systems<br />
  33. 33. Rajpat Systems<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×