Arrays in C#.Net

Array is a collection of similar data types. It is a sequential arrangements of our data and the data is stored with index starting from 0 index. We can use these index to access elements of an array. Arrays can be single dimensional, Multi dimensional or we can have arrays within array.

below is a syntax to declare array and an example of array.

[Data type][] variableName = new [Data type][size of array]

+
using System;
using System.Linq;
 
namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = {1,2,3,4,5};
            string[] str = { "Ravi""Ramesh""Sidhu" };
 
            Console.WriteLine("***** Printing numbers *****");
            for (int i = 0; i < num.Count(); i++)
            {
                Console.WriteLine(num[i]);
            }
 
            Console.WriteLine("\n***** Printing name *****");
            for (int j = 0; j < str.Count(); j++)
            {
                Console.WriteLine(str[j]);
            }
 
            //replacing element at index 0 with another element
            num[0] = 6;
 
            Console.WriteLine("\n***** Printing numbers *****");
            for (int i = 0; i < num.Count(); i++)
            {
                Console.WriteLine(num[i]);
            }
 
            num[5] = 7;
 
            Console.ReadLine();
        }
    }
}

In the above sample code we declared two single dimensional arrays num and str. num stores integer data and str array stores string data. We have initialised array at the declaration in {} bracket. Whatever Number of items we initialise there, that would be the max size of an array. In above example num array initialised with 5 elements and str array initialised with 3 elements. so this will be the maximum size of our array declared. so later in code we can't add more than 5 and 3 elements for our array num and str respectively. If I am trying to add element at 5th index position( num[5] = 7;) later it will give runtime error of out of index.
We can replace any array element with other element like in above code sample where i replaced element 1 of num array which is at index 0 with 6. You can see printed output with 6 at first position. See below output for our above code.



We can initialise the array size while declaring it and later add elements to this array upto the declared size of an array. See below code sample for the same where array size is declared as 10 at the time of declaration. So we can add max 10 elements for this array starting from index 0 till 9.

+
using System;
using System.Linq;
 
namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[10];
 
            num[0] = 1;
            num[1] = 2;
            num[2] = 3;
 
            Console.WriteLine("***** Printing numbers *****");
            for (int i = 0; i < num.Count(); i++)
            {
                Console.WriteLine(num[i]);
            }
 
            Console.ReadLine();
        }
    }
}

Below is the output for above code. If you see in above code we added only 3 elements in array. When we print this array, we can see that rest of elements are 0.

Arrays in C#

No comments :