SlideShare a Scribd company logo
Arrays❑ Simple arrays❑ Multidimensional arrays❑ Jagged arrays❑ The Array class❑ Interfaces for arrays❑ EnumerationsRajpat Systems
ArraysIf you need to work with multiple objects of the same type, you can use collections and arrays.Simple ArraysIf 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
Array Declaration & Array InitializationThe array cannot be resized after the size was specified without copying all elements.int[] myArray;myArray = new int[4];Using  an array initializerint[] myArray = new int[4] {4, 7, 11, 2};int[] myArray = new int[] {4, 7, 11, 2};int[] myArray = {4, 7, 11, 2};Rajpat Systems
Rajpat Systems
Accessing Array ElementsAfter 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 elementint v2 = myArray[1]; // read second elementmyArray[3] = 44; // change fourth elementIf you use a wrong indexer value where no element exists, an exception of type IndexOutOfRangeExceptionis thrown.Rajpat Systems
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
Using Reference TypesIn 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
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
Rajpat Systems
Multidimensional ArraysYou 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
Using an array initializerWhen 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
Rajpat Systems
Jagged ArraysA 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
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
Array ClassPropertiesRajpat Systems
Creating ArraysThe Array class is abstract, so you cannot create an array by using a constructor.Rajpat Systems
Rajpat Systems
Copying ArraysBecause 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
Rajpat Systems
If the array contains reference types, only the references are copiedRajpat Systems
SortingThe Array class implements a bubble - sort for sorting the elements in the array.\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
Rajpat Systems
If you are using custom classes with the array, you must implement the interface IComparable .This interface defines just one method, CompareTo().Rajpat Systems
Rajpat Systems
Array and Collection InterfacesThe 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
IEnumerableIEnumerable is an interface that is used by the foreach statement to iterate through the arrayRajpat Systems
ICollectionThe 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
Rajpat Systems
IListThe 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
Rajpat Systems
IEnumerator InterfaceThe foreach statement uses the methods and properties of the IEnumerator interface to iterate all elements in a collection.Rajpat Systems
foreach StatementThe 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
Rajpat Systems

More Related Content

What's hot (20)

PDF
intorduction to Arrays in java
Muthukumaran Subramanian
 
PPT
Java Arrays
Jussi Pohjolainen
 
PPT
Array in Java
Shehrevar Davierwala
 
PPT
Lec 25 - arrays-strings
Princess Sam
 
PPTX
Arrays in java language
Hareem Naz
 
PPT
Arrays Class presentation
Neveen Reda
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
Java arrays
BHUVIJAYAVELU
 
PDF
Java Arrays
OXUS 20
 
PPTX
Array
Anil Neupane
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
Arrays in java
Arzath Areeff
 
PPTX
Arrays in java
bhavesh prakash
 
PPTX
Java arrays
Jin Castor
 
PPT
C++ Arrays
أحمد محمد
 
PPTX
Two-dimensional array in java
Talha mahmood
 
PPTX
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
PPTX
Arrays In C++
Awais Alam
 
PPTX
Java arrays
Mohammed Sikander
 
PPT
Chap09
Terry Yoast
 
intorduction to Arrays in java
Muthukumaran Subramanian
 
Java Arrays
Jussi Pohjolainen
 
Array in Java
Shehrevar Davierwala
 
Lec 25 - arrays-strings
Princess Sam
 
Arrays in java language
Hareem Naz
 
Arrays Class presentation
Neveen Reda
 
Arrays in Java
Abhilash Nair
 
Java arrays
BHUVIJAYAVELU
 
Java Arrays
OXUS 20
 
Arrays in Java
Naz Abdalla
 
Arrays in java
Arzath Areeff
 
Arrays in java
bhavesh prakash
 
Java arrays
Jin Castor
 
C++ Arrays
أحمد محمد
 
Two-dimensional array in java
Talha mahmood
 
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
Arrays In C++
Awais Alam
 
Java arrays
Mohammed Sikander
 
Chap09
Terry Yoast
 

Viewers also liked (20)

DOC
Arrays In General
martha leon
 
PPT
Csc153 chapter 06
PCC
 
PPTX
Arrays
Peter Andrews
 
PDF
Learning VB.NET Programming Concepts
guest25d6e3
 
PDF
Portfolio -LouLou &amp; Tummie-
loulouandtummie
 
PPT
Exposure Lecture 2014 - Literature
mediaplaylab
 
PPT
England
guest0b5daf
 
PPT
A L BÚ M D E L P A R Q U E E C O LÓ G I C O
BAT007
 
PPT
Corinthians letroca 8 5A
sansampa
 
PDF
Performance of investment funds berhmani frigerio pan
BERHMANI Samuel
 
PPTX
6B Quadrinhos da Mônica
sansampa
 
PPT
Descascar letroca 03
sansampa
 
PPT
Character Development, for students, by students!
mediaplaylab
 
PPT
Learning with Quest Atlantis in Singapore
mediaplaylab
 
PDF
Second Life user guide
mediaplaylab
 
PPTX
презентация1
guest08adc2
 
PPTX
Epic product
Jakkathon Somkompa
 
PDF
Property Centric Media Kit
Property Centric
 
PDF
Bluesky Concept Presentation
HuanYang
 
Arrays In General
martha leon
 
Csc153 chapter 06
PCC
 
Learning VB.NET Programming Concepts
guest25d6e3
 
Portfolio -LouLou &amp; Tummie-
loulouandtummie
 
Exposure Lecture 2014 - Literature
mediaplaylab
 
England
guest0b5daf
 
A L BÚ M D E L P A R Q U E E C O LÓ G I C O
BAT007
 
Corinthians letroca 8 5A
sansampa
 
Performance of investment funds berhmani frigerio pan
BERHMANI Samuel
 
6B Quadrinhos da Mônica
sansampa
 
Descascar letroca 03
sansampa
 
Character Development, for students, by students!
mediaplaylab
 
Learning with Quest Atlantis in Singapore
mediaplaylab
 
Second Life user guide
mediaplaylab
 
презентация1
guest08adc2
 
Epic product
Jakkathon Somkompa
 
Property Centric Media Kit
Property Centric
 
Bluesky Concept Presentation
HuanYang
 
Ad

Similar to Array in C# 3.5 (20)

PDF
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
kamalabhushanamnokki
 
PPTX
arrays in c# including Classes handling arrays
JayanthiM19
 
DOCX
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPT
Arrays Basicfundamentaldatastructure.ppt
JyothiAmpally
 
PPTX
Module 7 : Arrays
Prem Kumar Badri
 
PDF
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
PPTX
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
PPTX
Arrays in programming
TaseerRao
 
DOC
Array properties
Shravan Sharma
 
PPTX
Intro to C# - part 2.pptx emerging technology
worldchannel
 
PPT
Md08 collection api
Rakesh Madugula
 
PPTX
Java Programming
Nanthini Kempaiyan
 
PPTX
collection framework.pptx
SoniaKapoor56
 
PPTX
unit 2.pptx
researchgrad82
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PDF
Java arrays (1)
Liza Abello
 
PPT
07 Arrays
maznabili
 
PDF
Lecture 6
Debasish Pratihari
 
PPT
Generics collections
Yaswanth Babu Gummadivelli
 
PPT
Generics Collections
phanleson
 
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
kamalabhushanamnokki
 
arrays in c# including Classes handling arrays
JayanthiM19
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Arrays Basicfundamentaldatastructure.ppt
JyothiAmpally
 
Module 7 : Arrays
Prem Kumar Badri
 
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
Arrays in programming
TaseerRao
 
Array properties
Shravan Sharma
 
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Md08 collection api
Rakesh Madugula
 
Java Programming
Nanthini Kempaiyan
 
collection framework.pptx
SoniaKapoor56
 
unit 2.pptx
researchgrad82
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Java arrays (1)
Liza Abello
 
07 Arrays
maznabili
 
Generics collections
Yaswanth Babu Gummadivelli
 
Generics Collections
phanleson
 
Ad

Array in C# 3.5

  • 1. Arrays❑ Simple arrays❑ Multidimensional arrays❑ Jagged arrays❑ The Array class❑ Interfaces for arrays❑ EnumerationsRajpat Systems
  • 2. ArraysIf you need to work with multiple objects of the same type, you can use collections and arrays.Simple ArraysIf 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 InitializationThe array cannot be resized after the size was specified without copying all elements.int[] myArray;myArray = new int[4];Using an array initializerint[] 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 ElementsAfter 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 elementint v2 = myArray[1]; // read second elementmyArray[3] = 44; // change fourth elementIf 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 TypesIn 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 ArraysYou 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 initializerWhen 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 ArraysA 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
  • 16. Creating ArraysThe Array class is abstract, so you cannot create an array by using a constructor.Rajpat Systems
  • 18. Copying ArraysBecause 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 copiedRajpat Systems
  • 21. SortingThe Array class implements a bubble - sort for sorting the elements in the array.\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 InterfacesThe 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. IEnumerableIEnumerable is an interface that is used by the foreach statement to iterate through the arrayRajpat Systems
  • 27. ICollectionThe 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. IListThe 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 InterfaceThe foreach statement uses the methods and properties of the IEnumerator interface to iterate all elements in a collection.Rajpat Systems
  • 32. foreach StatementThe 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