Order Now

C# Array

shape image

C# Array

C# Array

C# Array

The array is one kind of Data Structure. Let's know what kind of problem can be solved by an array. Suppose you want to store a student's roll number. That's very easy.
int roll = 12;
I just made a variable and assigned the roll number. What if the number of students was 100? Will you make 100 variables for 100 students then? It can't be a better way. This problem can be solved with the help of an array. An array can store the same type of multiple data at once.

Why need a C# Array?

C# arrays are a useful data structure that provides several advantages over other data structures. Some reasons why C# arrays are useful include:
  1. Efficient storage: Arrays are designed to store a large number of elements in a compact and efficient manner. This makes them ideal for storing large amounts of data, such as large lists or large sets of data.
  2. Random access: Arrays allow for fast and efficient random access to individual elements, using an index. This makes them ideal for scenarios where you need to access specific elements quickly, such as searching for a specific item in a list.
  3. Built-in methods: C# arrays provide several built-in methods for manipulating the elements of the array, such as sorting, reversing, and searching. These methods make it easy to perform common operations on arrays, without having to write your own code.
  4. Type safety: C# arrays are strongly typed, which means that each element of the array must be of the same type. This ensures that the elements of the array can be safely used and manipulated without the risk of type errors.
  5. Multi-dimensional arrays: C# also supports multi-dimensional arrays such as 2D and 3D arrays, and jagged arrays, which are arrays of arrays. These arrays are useful for storing data that has multiple dimensions, such as a 3D game map or a 2D spreadsheet.
  6. Interoperability with other C# constructs: Arrays are a fundamental construct in C#, and they are widely used in various C# constructs like loops, classes, methods, etc. Arrays are also used in conjunction with other C# features like generics, delegates, and lambda expressions.


Array declaration:
int[] rolls = new int[5];
Here 5 is the size of this array.

Add items to the Array:
            rolls[0] = 100;
            rolls[1] = 200;
            rolls[2] = 300;
            rolls[3] = 400;
            rolls[4] = 500;
Here 0,1 and 2 are index numbers of the array. I have assigned a 100 to 0 number index, a 200 into 1 number index, a 300 to 2 number index, a 400 into 3 number index, and a 500 into 4 number index. We can't assign in the 5th index because the size of the array is 5 and 0 to 4 is equal to 5.

Declaration & Add items to the Array in the same line:

We can declare the array and add value in the same line also. And it is easy & clear too.
int[] rolls = new int[5] {100,200,300,400,500};

If we declare and add value together, then there is no problem if we do not give the size of the array.
Console.WriteLine(rolls[3]);

Print single value:
Console.WriteLine(rolls[3]);

Print all values using for loop:
 for(int i=0; i < rolls.Length; i++)
  {
      Console.WriteLine(rolls[i]);
  }

Print all values using the foreach loop:
foreach (int roll in rolls)
 {
    Console.WriteLine(roll);
 } 

Here is all the code.
             ////Declaration
            //int[] rolls = new int[5];

            ////Value Assign
            ///
            //rolls[0] = 100;
            //rolls[1] = 200;
            //rolls[2] = 300;
            //rolls[3] = 400;
            //rolls[4] = 500;

            ////Declaration & Value Assign in same line
            //int[] rolls = new int[5] { 100, 200, 300, 400, 500 };

            //Declaration & Value Assign in same line without size
            int[] rolls = new int[] {100,200,300,400,500};

            for (int i = 0; i < rolls.Length; i++)
            {
                Console.WriteLine(rolls[i]);
            }

            foreach (int roll in rolls)
            {
                Console.WriteLine(roll);
            }

2-Dimensional (2D)  Array

Do you know Matrix? If you understand the matrix, you will understand it very well. Don't worry. Let's understand without it. A 2-Dimensional Array means the data will be stored in rows and columns.
2-Dimensional Array


Value assigns with index number:
int[,] TwoD = new int[2, 3];
            TwoD[0, 0] = 10;
            TwoD[0, 1] = 20;
            TwoD[0, 2] = 30;
            TwoD[1, 0] = 10;
            TwoD[1, 1] = 20;
            TwoD[1, 2] = 30;


Iterate over the 2D Array (for loop):
static void Main(string[] args)
        {
            int[,] twod = new int[2, 3] {
                                          {10, 20, 30},
                                          {40, 50, 60}
                                        };

            for (int i=0; i < twod.GetLength(0); i++)
            {
                for(int j=0; j < twod.GetLength(1); j++)
                {
                    Console.WriteLine(twod[i, j]);
                }
            }
        }

Please note that this time the "GetLength" is used instead of "Length" in the condition part of "for loop". The Outer "for loop" is responsible for the iteration of Rows and the inner "for loop" is responsible for the iteration of Columns.

Iterate over the 2D Array (foreach loop):
static void Main(string[] args)
        {
            int[,] twod = new int[2, 3] {
                                          {10, 20, 30},
                                          {40, 50, 60}
                                        };

            foreach (var item in twod)
            {
                Console.WriteLine(item);
            }
        }

3D or Multi-Dimensional Array

3D array means a collection of the 2D array. Here data will be stored on the basis of Block Size, Row Size, and Column Size.

3D Array


Value assigns with index number:
int[,,] threeD = new int[2, 3, 4];
            threeD[0, 0, 0] = 10;
            threeD[0, 0, 1] = 20;
            threeD[0, 0, 2] = 30;
            threeD[0, 0, 3] = 40;

            threeD[0, 1, 0] = 50;
            threeD[0, 1, 1] = 60;
            threeD[0, 1, 2] = 70;
            threeD[0, 1, 3] = 80;

            threeD[0, 2, 0] = 90;
            threeD[0, 2, 1] = 100;
            threeD[0, 2, 2] = 200;
            threeD[0, 2, 3] = 300;

            threeD[1, 0, 0] = 400;
            threeD[1, 0, 1] = 500;
            threeD[1, 0, 2] = 600;
            threeD[1, 0, 3] = 700;

            threeD[1, 1, 0] = 800;
            threeD[1, 1, 1] = 900;
            threeD[1, 1, 2] = 1000;
            threeD[1, 1, 3] = 2000;

            threeD[1, 2, 0] = 3000;
            threeD[1, 2, 1] = 4000;
            threeD[1, 2, 2] = 5000;
            threeD[1, 2, 3] = 6000;


Iterate over the 3D Array (for loop):
static void Main(string[] args)
        {
            int[,,] threeD = new int[2, 3, 4]
            {
                {
                   {10,20,30,40},
                   {50,60,70,80},
                   {90,100,200,300}
                },
                {
                   {400,500,600,700},
                   {800,900,1000,2000},
                   {3000,4000,5000,6000}
                }
            };

            for (int i=0; i<threeD.GetLength(0); i++)
            {
                for (int j=0; j<threeD.GetLength(1); j++)
                {
                    for (int k=0; k<threeD.GetLength(2); k++)
                    {
                        Console.WriteLine(threeD[i, j, k]);
                    }
                }
            }

        }

The Outer "for loop" is responsible for the iteration of Blocks, The Middle "for loop" is responsible for the iteration of Rows, and The Inner "for loop" is responsible for the iteration of Columns.

Iterate over the 3D Array (foreach loop):
static void Main(string[] args)
        {
            int[,,] threeD = new int[2, 3, 4]
            {
                {
                   {10,20,30,40},
                   {50,60,70,80},
                   {90,100,200,300}
                },
                {
                   {400,500,600,700},
                   {800,900,1000,2000},
                   {3000,4000,5000,6000}
                }
            };

            foreach (int item in threeD)
            {
                Console.WriteLine(item);
            }
        }

Problems in Array

The array is a good data structure but it has two limitations.
  1. Type is fixed: You can't store multiple types of data. Example: You can't store 10 and "Zakir" in the same array.
  2. Size is fixed: The array is not scalable. If you make an array for 60 students, you can't store 61 students' data.

Post a Comment

© Copyright 2023 ZakirDev

Order Form

I will contact you on WhatsApp.

Order now