SlideShare a Scribd company logo
Arrays in C#
By Vanitha
Introduction
 Array in C# is a group of similar types of elements that have contiguous memory
location.
 In C#, array is an object of base type System.Array.
 In C#, array index starts from 0. We can store only fixed set of elements in C# array.
 Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
Array(cont..)
Advantages of C# Array
• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.
Disadvantages of C# Array
• Fixed size
Array(cont..)
Advantages of C# Array
• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.
Disadvantages of C# Array
• Fixed size
Structure of Array
Types of Array
There are 3 types of arrays in C# programming:
1. Single Dimensional Array
2. Multidimensional Array
3. Jagged Arrays
C# Single Dimensional Array-Initialization
 To create single dimensional array, you need to use square brackets [] after
the type.
int[] arr = new int[5]; //creating array
 string[] cars = {"Volvo", "BMW",
"Ford", "Mazda"};
Console.WriteLine(cars[0]);
//Outputs Volvo
Example of a Single Dimensional Array
public class single
{
public static void Main(string[] args)
{
int[] arr = new int[5]; //creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console. WriteLine(arr[i]);
}
}
}
Output is:
10
0
20
0
30
Multidimensional Arrays
 The multidimensional array is also known as
rectangular arrays in C#. It can be two dimensional or
three dimensional. The data is stored in tabular form
(row * column) which is also known as matrix.
 To create multidimensional array, we need to use
comma inside the square brackets. For example
1. int[,] arr=new int[3,3];//declaration of 2D array
2. int[,,] arr=new int[3,3,3];//declaration of 3D array
Example of Multidimensional
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine(); //new line at each row
}
} }
Output is:
0 10 0 0 0 20 30 0 0
C# Jagged Arrays
 In C#, jagged array is also known as "array of arrays" because its elements are arrays. The
element size of jagged array can be different.
 int[][] arr = new int[2][];
Initialization:
1.arr[0] = new int[4];
2.arr[1] = new int[6];
Example of jagged array
public class JaggedArrayTest
{
public static void Main()
{
int[][] arr = new int[2][];// Declare the array
arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
// Traverse array elements
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write(arr[i][j]+" ");
}
System.Console.WriteLine();
}
}
}
Output:
11 21 56 78 42 61 37 41 59 63

More Related Content

Similar to Arrays in .Net Programming Language.pptx (20)

PPT
CSharp_02_Arrays_fundamentals_concepts_introduction
Ranjithsingh20
 
PPTX
C# Lesson 3
Bohdan Pashkovskyi
 
PDF
Intake 38 3
Mahmoud Ouf
 
PPTX
C# Array.pptx
ZanyarRzgar
 
PPTX
Arrays.pptx
Mohit750936
 
PPT
07 Arrays
maznabili
 
PPTX
Arrays, Structures And Enums
Bhushan Mulmule
 
PPTX
Module 7 : Arrays
Prem Kumar Badri
 
PPT
Csharp4 arrays and_tuples
Abed Bukhari
 
PDF
DSA-Lecture-05
Haitham El-Ghareeb
 
PPTX
Visual Programing basic lectures 7.pptx
Mrhaider4
 
PPTX
Intro to C# - part 2.pptx emerging technology
worldchannel
 
PPTX
Arrays C#
Raghuveer Guthikonda
 
PPTX
Array
Allah Ditta
 
PDF
C sharp chap6
Mukesh Tekwani
 
PPT
Visula C# Programming Lecture 5
Abou Bakr Ashraf
 
PDF
LectureNotes-05-DSA
Haitham El-Ghareeb
 
PPTX
ARRAYS.pptx
akila m
 
PDF
C# p9
Renas Rekany
 
CSharp_02_Arrays_fundamentals_concepts_introduction
Ranjithsingh20
 
C# Lesson 3
Bohdan Pashkovskyi
 
Intake 38 3
Mahmoud Ouf
 
C# Array.pptx
ZanyarRzgar
 
Arrays.pptx
Mohit750936
 
07 Arrays
maznabili
 
Arrays, Structures And Enums
Bhushan Mulmule
 
Module 7 : Arrays
Prem Kumar Badri
 
Csharp4 arrays and_tuples
Abed Bukhari
 
DSA-Lecture-05
Haitham El-Ghareeb
 
Visual Programing basic lectures 7.pptx
Mrhaider4
 
Intro to C# - part 2.pptx emerging technology
worldchannel
 
C sharp chap6
Mukesh Tekwani
 
Visula C# Programming Lecture 5
Abou Bakr Ashraf
 
LectureNotes-05-DSA
Haitham El-Ghareeb
 
ARRAYS.pptx
akila m
 

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Ad

Arrays in .Net Programming Language.pptx

  • 1. Arrays in C# By Vanitha
  • 2. Introduction  Array in C# is a group of similar types of elements that have contiguous memory location.  In C#, array is an object of base type System.Array.  In C#, array index starts from 0. We can store only fixed set of elements in C# array.  Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
  • 3. Array(cont..) Advantages of C# Array • Code Optimization (less code) • Random Access • Easy to traverse data • Easy to manipulate data • Easy to sort data etc. Disadvantages of C# Array • Fixed size
  • 4. Array(cont..) Advantages of C# Array • Code Optimization (less code) • Random Access • Easy to traverse data • Easy to manipulate data • Easy to sort data etc. Disadvantages of C# Array • Fixed size
  • 6. Types of Array There are 3 types of arrays in C# programming: 1. Single Dimensional Array 2. Multidimensional Array 3. Jagged Arrays
  • 7. C# Single Dimensional Array-Initialization  To create single dimensional array, you need to use square brackets [] after the type. int[] arr = new int[5]; //creating array  string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; Console.WriteLine(cars[0]); //Outputs Volvo
  • 8. Example of a Single Dimensional Array public class single { public static void Main(string[] args) { int[] arr = new int[5]; //creating array arr[0] = 10;//initializing array arr[2] = 20; arr[4] = 30; //traversing array for (int i = 0; i < arr.Length; i++) { Console. WriteLine(arr[i]); } } } Output is: 10 0 20 0 30
  • 9. Multidimensional Arrays  The multidimensional array is also known as rectangular arrays in C#. It can be two dimensional or three dimensional. The data is stored in tabular form (row * column) which is also known as matrix.  To create multidimensional array, we need to use comma inside the square brackets. For example 1. int[,] arr=new int[3,3];//declaration of 2D array 2. int[,,] arr=new int[3,3,3];//declaration of 3D array
  • 10. Example of Multidimensional using System; public class MultiArrayExample { public static void Main(string[] args) { int[,] arr=new int[3,3];//declaration of 2D array arr[0,1]=10;//initialization arr[1,2]=20; arr[2,0]=30; //traversal for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ Console.Write(arr[i,j]+" "); } Console.WriteLine(); //new line at each row } } } Output is: 0 10 0 0 0 20 30 0 0
  • 11. C# Jagged Arrays  In C#, jagged array is also known as "array of arrays" because its elements are arrays. The element size of jagged array can be different.  int[][] arr = new int[2][]; Initialization: 1.arr[0] = new int[4]; 2.arr[1] = new int[6];
  • 12. Example of jagged array public class JaggedArrayTest { public static void Main() { int[][] arr = new int[2][];// Declare the array arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array arr[1] = new int[] { 42, 61, 37, 41, 59, 63 }; // Traverse array elements for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr[i].Length; j++) { System.Console.Write(arr[i][j]+" "); } System.Console.WriteLine(); } } } Output: 11 21 56 78 42 61 37 41 59 63