Arrays:

1.Arrays in C# are used for storing multiple related values under a single variable name. 2.Arrays eliminate memory wastage. 3.Array is a complex data type (complex data types can store more than one value). 4.Arrays are reference types in C#.

Types of arrays:

1.one-dimensional arrays. 2.multi-dimensional arrays. 3.jagged arrays.

syntax for declaring one-dimensional arrays:

There are different ways of declaring arrays in C#.
data-type[] variable-name=new data-type[size]{element1,element2,...};
The above syntax declares an array with specified size and loads specified elements into the array.
data-type[] variable-name=new data-type[]{element1,element2,...};
When the array declaration and assigning data happens in same line then the size is optional.
The following syntax shows the mechanism for declaring an empty array(specifying the size is mandatory for declaring empty arrays).
data-type[] variable-name=new data-type[size];

Reading and modifying data present in arrays:

The following code declares a string array which contains 6 strings.
 
string[] dNames = new string[6] {"Dr.Yuvi","Dr.Kate","Dr.Dappu","Dr.Vani","Dr.Bill","Dr.Sara"};
The given string array appears in the memory as shown below.
string-array-image
Array is a complex data type, hence we can't use variable name alone for displaying entire data present in an array. Hence following code will not display the data present in array, instead it will display data type of dNames variable that is string[].
Console.WriteLine(dNames)
Use array-variable-name[index-number] for displaying data present in any array cell.see following code for the sample.
public class Test
{
    public static void Main(string[] args)
    {
        string[] dNames = new string[6]{"Dr.Yuvi","Dr.Kate","Dr.Dappu","Dr.Vani","Dr.Bill","Dr.Sara"};
        Console.WriteLine(dNames[0]);
        Console.WriteLine(dNames[1]);
        Console.WriteLine(dNames[2]);
        Console.WriteLine(dNames[3]);
        Console.WriteLine(dNames[4]);
        Console.WriteLine(dNames[5]);
    }
}
  • There is a repitition in the above given code.
  • To avoid repetition, use a loop like for, for-each or while.
For more details see loops tutorial. To modify existing value or assign a new value to any array's cell use the following syntax.
dNames[index-number or cell-number]=new-value;
The following code modifies the 3rd cell value.
dNames[2]="Dr.Bryan";

Passing arrays to Methods and returning arrays from methods:

Methods in C# can take arrays as input and can return arrays as output. In the following sample, two arrays are passed to GetAvgMarks method. This method returns an array as output which contains the average marks.
public class Test
{
    public static void Main(string[] args)
    {
        int[] frstSemMrks = new int[] { 80, 90, 60, 78 };
        int[] scndSemMrks = new int[] { 90, 60, 50, 48 };
        /*Pass the current arrays which contains a students two semisters marks */
        MarksCalc mc = new MarksCalc();
        int[] avgMarks=mc.CalculateAvgMarks(frstSemMrks, scndSemMrks);
        for (int i = 0; i < avgMarks.Length; i++)
        {
            Console.WriteLine(avgMarks[i]);
        }
    }
}
public class MarksCalc 
{
    public int[] CalculateAvgMarks(int[] firstSemMarks, int[] secondSemMarks)
    {
        /*Declare an empty array for storing the calculated aggregated marks */
        int[] avgMarks = new int[firstSemMarks.Length];
        for (int i = 0; i < avgMarks.Length; i++)
        {
            avgMarks[i] = (firstSemMarks[i] + secondSemMarks[i]) / 2;
        }
        return avgMarks;
    }
}

Assignments on Arrays and Methods:

1.Perform reverse multiplication on the given arrays.
public class Test
{
    public static void Main(string[] args)
    {
        int[] nums1= new int[] { 8, 9, 6, 7};
        int[] nums2= new int[] { 9, 6, 5, 4 };
        /*Pass the current arrays which contains a students two semisters marks */
        ArrayCalc mc = new ArrayCalc();
        int[] res=mc.ReverseMultiply(nums1, nums2);
        for (int i = 0; i < res.Length; i++)
        {
            Console.WriteLine(avgMarks[i]);
        }
       /*Expected output 32,45,36,63 */
    }
}
public class ArrayCalc 
{
    public int[] ReverseMultiply(int[] n1,int[] n2)
    {
        /* Declare an empty array for storing the calculated values. */
           int[] res = new int[n1.Length];
        /* complete the remaining code */
    }
}

Multi-dimensional Arrays:

C# language supports multi dimensional arrays. The following code sample is an example for 2 dimenional array.
int[,] nums=new int[3,2]{{10,8},{-2,5},{1,3}};
multi-dimensional-array Use the following code for reading the data present in the above given multi dimensional array.
for(int i=0;i < nums.Length;i++)
{
  Console.WriteLine(nums[i,0]);
  Console.WriteLine(nums[i,1]);
}

Jagged Arrays:

Jagged array is an array of array's.
int[][] jaggedArray = new int[][]{new int[] {1,3,5,7,9},new int[] {0,2,4,6},new int[] {11,22}};
The following sample will display the data present in the given jagged array.
public class ArrayTest
{
    public static void Main(string[] args)
    {
        int[][] jaggedArray = new int[][]{new int[] {1,3,5,7,9},new int[] {0,2,4,6},
                                           new int[] {11,22}};
        for(int i=0;i < jaggedArray.Length;i++)
        {
          for(int j=0;j < jaggedArray[i].Length;j++)
          {
            Console.WriteLine(jaggedArray[i][j]);
          }
        }
    }
}