Udemy logo

c sharp array

An array is a data type that holds a sequence of values of the same type. The length of the array is defined at the time of array declaration. Each item in an array is called an element and each element of an array can be accessed using an array index. The index value of an array starts with zero and ends with the length of the array minus one.

Learn more about C# by taking a course at Udemy.com

The following data types can be used in C#:

An array declaration has two parts: array type and array name. The array type defines the type of data you store in the array elements. In the above samples, notice the brackets. They are what tells the compiler that you’re declaring an array of that data type.

 

Initializing and Accessing an Array

There are two ways to create an array. You can use the “new” operator or declare the array with static data.

Learn C# from scratch at Udemy.com

double i_an array= new double[10];

The above statement declares a double array and new operator allocates the index size, which in this example is 10.

Alternatively, we can write a declaration with initialization

double[] an_array={10.01,20.02,30.03,40.02,50.1,60.02,70.05,80.07,90.08};
int [] mark = new int[6]  { 9,  8, 2, 97, 5};

In this example, the number of values given at initialization time determines the length. Once the array has been declared, its size cannot be changed later in the code.

Example of Defining, Storing and Retrieving Values from an Array:

class ArrayDemo
{
public static void main(String[] args)
{
// declaring an integer array
int[] amArray;
// Allocating memory for 10 elements of array
amArray = new int[10];
amArray[0] = 1;// Initializing the first element
amArray[1] = 2;// Initializing the second element
amArray[2] = 3;
amArray[3] = 4;
amArray[4] = 5;
amArray[5] = 6;
Console.WriteLine("Element at index 0: "+ amArray[0]);
Console.WriteLine("Element at index 1: "+ amArray[1]);
Console.WriteLine("Element at index 2: "+ amArray[2]);
Console.WriteLine("Element at index 3: "+ amArray[3]);
Console.WriteLine("Element at index 4: "+ amArray[4]);
Console.WriteLine("Element at index 4: "+ amArray[5]);
}
}

Output:

Element at index 0: 1

Element at index 1: 2

Element at index 2: 3

Element at index 3: 4

Element at index 4: 5

Element at index 5: 6

Generally, looping statements are used to dynamically assign values of an array, which prevents writing an individual statement for every index. We can also declare dynamic array by asking the user of the size of the array and then declaring the array.

Learn C# in an hour at Udemy.com

Parsing and Creating a Dynamic Array Using a Loop

using System;
public class Test
{
public static void Main()
{
// your code goes here
int[] amArray;
Console.WriteLine("Enter size of an Array");
int a=Convert.ToInt32(Console.ReadLine());
amArray = new int[a];
Console.WriteLine("Element at index {0}",a);
Console.WriteLine("Enter elements of Array");
for(int x=0;x<a;x++)
{
amArray[x]=Convert.ToInt32(Console.ReadLine());// Integer input array elements
}
for(int x=0;x<a;x++)// Printing the Array Elements
{
Console.WriteLine("Element at index {0} {1}: ",x,amArray[x]);
}
}
}

Output

Enter size of an Array 5

Enter elements of Array 100 200 300 400 500

Element at index 0 100:

Element at index 1 200:

Element at index 2 300:

Element at index 3 400:

Element at index 4 500:

Double Dimensional Array: These types of arrays store elements of same data type in the form of rows and column.

Syntax:

int [][] am_array=new int[2][5];

The above statement declares a double dimensional array of size 10. The first bracket denotes the row while the second bracket denotes the number of columns. With even double dimensional array, the index number starts from zero.

Example:

using System;
public class Test
{
public static void Main()
{
// Memory allocation for 6 integers
int [,] amArray = new int[2,4];
// initialize element of first row and first column
amArray[0,0] = 100;
// initialize element of first row and second column
amArray[0,1] = 200;
// and so forth
amArray[0,2] = 300;
amArray[0,3] = 400;
amArray[1,0] = 500;
amArray[1,1] = 600;
amArray[1,2] = 700;
amArray[1,3] = 800;
for(int x=0;x<2;x++) // Parsing the double dimensional Array
{
for(int y=0;y<4;y++)
{
Console.WriteLine("Element at index {0}  Row and {1}  Column: {2} ", x, y, amArray[x,y]);
}
}
}
}

Output:

Element at index 0  Row and 0  Column: 100

Element at index 0  Row and 1  Column: 200

Element at index 0  Row and 2  Column: 300

Element at index 0  Row and 3  Column: 400

Element at index 1  Row and 0  Column: 500

Element at index 1  Row and 1  Column: 600

Element at index 1  Row and 2  Column: 700

Element at index 1  Row and 3  Column: 800

Jagged Array: The jagged arrays are the array of array.

Jagged Array Declaration and Initialization:

int [][] runs;
int[][] runs = new int[2][]{new int[]{12,13,14},new int[]{15,16,17,18},new int[]{19,20,21}};

The “runs” is array of three array of integer.

runs[0] is an array of 3 integers

runs[1] is an array of 4 integers

runs[2] is an array of 3 integers

 

Example of Jagged Arrays:

using System;
namespace Demo_pplication
{
class MyArray
{
static void Main(string[] args)
{
int[][] a = new int[][]{new int[]{1,2},new int[]{10,20},
new int[]{30,40},new int[]{ 50, 60 }, new int[]{ 70, 80 } }; //Jagged Array Declaration
int x, y;
for (x= 0; x < 5; x++)
{
for (y = 0; y < 2; y++)
{
Console.WriteLine("Jagged Array a[{0}][{1}] = {2}", x, y, a[x][y]); //Printing Jagged Array
}
}
}
}
}

Output

Jagged Array a[0][0] = 2

Jagged Array a[0][1] = 1

Jagged Array a[1][0] = 10

Jagged Array a[1][1] = 20

Jagged Array a[2][0] = 30

Jagged Array a[2][1] = 40

Jagged Array a[3][0] = 50

Jagged Array a[3][1] = 60

Jagged Array a[4][0] = 70

Jagged Array a[4][1] = 80

Array Functions:

Length: Provides the length of an array i.e. number of elements in an array

reverse(): Reverses the array elements i.e. last elements comes to first index and vice versa.

sort(): Sorts the array in ascending and descending form.

Example

using System;
namespace MyArrayFunctions
{
class MyArrayFunctions
{
static void Main(string[] args)
{
int[] abc = { 15,16,24,56,4,10,80,5,80,100};
int[] abc1 = abc;
int len=abc.Length;
Console.Write("Number of Elements are {0}",len);
Console.Write("Original Array: ");
foreach (int x in abc)
{
Console.Write(x + " ");
}
Console.WriteLine();
//Reversing The Array
Array.Reverse(abc1);
Console.Write("Reversed Array: ");
foreach (int i in abc1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//sorting the array
Array.Sort(abc);
Console.Write("Array Sorted: ");
foreach (int i in abc)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}

Output

Number of Elements are 10

Original Array: 15 16 24 56 4 10 80 5 80 100

Reversed Array: 100 80 5 80 10 4 56 24 16 15

Sorted Array: 4 5 10 15 16 24 56 80 80 100

Page Last Updated: May 2014

Top courses in C# (programming language)

Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.6 (31,240)
.NET / C# Interview Questions with Answers.
Shivprasad Koirala
4.7 (2,007)
Advanced Topics in C#
Dmitri Nesteruk
4.4 (809)
Complete C# Unity Game Developer 3D
Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (42,778)
Bestseller
Design Patterns in C# and .NET
Dmitri Nesteruk
4.5 (12,399)
Bestseller
Ultimate C# Masterclass for 2024
Krystyna Ślusarczyk
4.7 (4,177)
Bestseller
C# 11 - Ultimate Guide - Beginner to Advanced | Master class
Web University by Harsha Vardhan
4.6 (4,095)

More C# (programming language) Courses

C# (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo