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