SlideShare a Scribd company logo
1 of 11
Arrays
❑ Simple arrays
❑ Multidimensional arrays
❑ Jagged arrays
Arrays
• If you need to work with multiple objects of
the same type, you can 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.
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};
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.
– 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
IndexOutOfRangeException is thrown.
• 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
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;
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.
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 };
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]
);
}
}
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.

More Related Content

What's hot (20)

An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Java arrays
Java arraysJava arrays
Java arrays
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
 
Arrays
ArraysArrays
Arrays
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
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
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Array lecture
Array lectureArray lecture
Array lecture
 
7array in c#
7array in c#7array in c#
7array in c#
 
2 dimension array in programms
2 dimension array in programms2 dimension array in programms
2 dimension array in programms
 
12. arrays
12. arrays12. arrays
12. arrays
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Learn Java Part 8
Learn Java Part 8Learn Java Part 8
Learn Java Part 8
 

Similar to C# Lesson 3 (20)

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
 
Array
ArrayArray
Array
 
javaarray
javaarrayjavaarray
javaarray
 
Python array
Python arrayPython array
Python array
 
Arrays
ArraysArrays
Arrays
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
L10 array
L10 arrayL10 array
L10 array
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)Arrays in java (signle dimensional array)
Arrays in java (signle dimensional array)
 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Basic Arrays in C Programming Language I
Basic Arrays in C Programming Language IBasic Arrays in C Programming Language I
Basic Arrays in C Programming Language I
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Array-part1
Array-part1Array-part1
Array-part1
 
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
 
Array
ArrayArray
Array
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
Array
ArrayArray
Array
 

More from Bohdan Pashkovskyi

More from Bohdan Pashkovskyi (9)

Telegram bots
Telegram botsTelegram bots
Telegram bots
 
CoreCamp "Automated testing basics for developers"
CoreCamp "Automated testing basics for developers"CoreCamp "Automated testing basics for developers"
CoreCamp "Automated testing basics for developers"
 
CQRS and Event Sourcing
CQRS and Event SourcingCQRS and Event Sourcing
CQRS and Event Sourcing
 
C# Lesson 1
C# Lesson 1C# Lesson 1
C# Lesson 1
 
Automated testing
Automated testingAutomated testing
Automated testing
 
IF .NET User Group
IF .NET User GroupIF .NET User Group
IF .NET User Group
 
Asynchronous programming in C#
Asynchronous programming in C#Asynchronous programming in C#
Asynchronous programming in C#
 
ASP .NET MVC - best practices
ASP .NET MVC - best practicesASP .NET MVC - best practices
ASP .NET MVC - best practices
 
.Net Core
.Net Core.Net Core
.Net Core
 

Recently uploaded

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 

Recently uploaded (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 

C# Lesson 3

  • 1. Arrays ❑ Simple arrays ❑ Multidimensional arrays ❑ Jagged arrays
  • 2. Arrays • If you need to work with multiple objects of the same type, you can 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.
  • 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};
  • 4. 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. – 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 IndexOutOfRangeException is thrown.
  • 5. • 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
  • 6. 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;
  • 7. 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.
  • 8.
  • 9. 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 };
  • 10. 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] ); } }
  • 11. 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.