Arrays in C#


Introduction
A normal variable can store any single data of same type whereas an array is a single data variable that can store multiple pieces of data each of the same type. Each of these elements is stored sequentially using index values in the computer's memory. In C#, index value starts with 0. The basic format of an array is given below:
datatype[] variablename;
'datatype' indicates the type of information array will store namely int, char etc. The square bracket indicates that we are declaring array type variable and 'variablename' is the name of the array variable.
decimal[] money;
Above declaration creates variable that will hold decimal values, however we didn't set the piece of information to be stored. Let's do this by this code:
decimal[] money;
money = new decimal[8];
Above code is using declaration and initialization in two different lines, we can do this at the same time, as given below:
decimal[] money = new decimal[8];
In above code, money array declares 8 elements and the index starts with 0 and ends at 7.
Now, to access the specific element within array, we need to use coding something like:
money[0] = 1276.46;
money[1] = 435.55;
:::::::::::::::::::::::
money[7] = 65.76;
It is very common mistake made by some Visual Basic programmers because in Visual Basic, we can start with an index of 1, however most languages including C# start with an index of 0.
Sample Program on Array
using System;
class Program
{
    static void Main(string[] args)
    {
        decimal[] money = new decimal[8];
        System.Random rnd = new System.Random();
        for (int i = 0; i < 8; i++)
        {
            money[i] = (decimal)(rnd.NextDouble());
        }
        for (int i = 0; i < 8; i++)
        {
            Console.WriteLine(money[i]);
        }
        Console.ReadKey();
    }
}

Initializing Array at declaration time
We can initialize the value of the individual array element at the same time that we declare and initialize the array, here you go.
decimal[] money = new decimal[8] { 12.5, 3.6, 76.8, 34.698, 300.65, 6.6, 43.8, 98.2 };
We don't require initializing all the values for array, here we are just initializing only 3 elements.
decimal[] money = new decimal[8] { 12.6, 54.76 };
We can't initialize more values left above. The same technique applies on array which has no size defined.
decimal[] money = new decimal[] { 76.43, 768.4 };
Above code has just 2 piece of information and we can add more information later.
Sample Program
using System;
class Program
{
    static void Main(string[] args)
    {
        char[] name = new char[] {'A','b','h','i','m','a','n','y','u', (char) 0 };
        int ctr = 0;
        while (name[ctr] != 0)
        {
            Console.Write("{0}", name[ctr]);
            ctr++;
        }
        Console.ReadKey();
    }
}

Multidimensional Array

Multidimensional array may be defined as "it is array of array" or even we can have "array of array of array" and more, if you go ahead it starts getting complicated. Actually an array of array is referred to as two-dimensional array because it can be represented in two dimensions. To declare two dimensional arrays, we expand what we do with regular array or one dimensional array:

int[,] marks = new int[5, 3];
In above example, a comma is added to the first part of the declaration and two numbers separated by a comma, it creates two dimensional arrays that have 5 elements and each containing an array of 3 elements. So, it total marks variable can hold 5 (rows) x 3 (columns) = 15 (elements). Above code is just declaration of array not initialization of values. Let's initialize some value now.
int[,] marks = new int[5, 3] {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}};
Look at the pictorial representation of my screen.
In above example, we are initializing all cells once; we can do this as given below too.
int[,] marks;
marks = new int[5,3];
marks[0,0] = 1;
marks[0,1] = 2;
marks[0,2] = 3;
marks[1,0] = 4;
marks[1,1] = 5;
:::::::::::::::::::
marks[4,0] = 13;
marks[4,1] = 14;
marks[4,2] = 15;
Look at the pictorial representation of my screen for this type of initialization.
Now to view cell values on screen as output we need to have nested loops, here you go.
Array can contain another array too. Let's go to have look over this too.
char[][] name = new char[3][];
name[0] = new char[] {'A','b','h','i','m','a','n','y','u'};
name[1] = new char[] {'K', 'u', 'm', 'a', 'r'};
name[2] = new char[] {'V', 'a', 't', 's', 'a'};
In above example, name array is an array of arrays. It contains three character arrays that are each a different length. Because they are different lengths, we work with their elements differently from the rectangular arrays that we saw before.
Instead of addressing each element by using index values separated by commas, we instead separate the elements into their own square brackets. Look at the example:
Console.WriteLine("{0}{1}{2}", name[0][0], name[1][0], name[2][0]);
Above, console will pick every first element from the name array and the output will be "AKV".
That's all about the array in C#. I hope you would like this. Please post your comments.

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples