Hello Friends, In this tutorial, I am going to explain Array in C#. We can say Array as a collection of similar type of datatype. Also, we will create examples on Array.

After completing this tutorial you will be able to understand:

  • Array in C#.
  • How to declare Array in C#.
  • How to Initialize Array in C#.
  • How to Access Array item using Index.

 

Introduction:

Array in C# is used to store similar type of datatype. We can access Array item using the index.  Array index starts with zero.  As a beginner, you can think array as a collection of the variables of the same datatype.  For E.g. if you want to store 10 values then instead of declaring 10 variables like int num1,num2,…., num10, you can declare one array of size 1 and can access those array item like num[0], num[1],…, num[10].

Array

The syntax for declaring Array in C#.

datatype[] arrayname;

Where,

  • datatype specifies the type of element that array will consist such as int, string etc.
  • [] specifies the size of the array.
  • arrayname specifies the name of the array.

An example of declaring Array:

sting[] StudentName;

int[] RollNo;

double[] Fee;

Initializing Array:

An array is initialized using the new keyword as shown in the below example.

string[] StudentName=new string[2];

int[] RollNo=new int[2];

double[] Fee=new double[2];

Assigning Values to an Array:

You can assign a value to an array using the index.

String Array:

string[] StudentName=new string[2];

StudentName[0]="Vinod";

StudentName[1]="Ansh";

Int Array:

int[] RollNo=new int[2];

RollNo[0]=1;

RollNo[1]=2;

double Array:

double[] Salary=new double[2];

Salary[0]=200;

Salary[1]=500;

You can also assign values to an Array item at the time of declaration.

String Array:

string StudentName[]=new string {"Vinod","Ansh"};

int Array:

int[] RolNo=new int {1,2};

double Array:

double[] Fee=new double {200,500};

Complete Example of Array in C#:

In this example, We will store numbers from 1 to 10 in an array and print those number using array index as we discussed above.

Open Visual Studio and create a Console Application with name ArrayDemo.

Array

int[] iNumber=new int[10];
 //intialize Array item
 Console.WriteLine("Storing Values to Array");
 for (int iCount = 1; iCount <= 10; iCount++)
 {
 //Since array index starts from 0 so we have subtract 1 from iCount
 Console.WriteLine("Storing Value at Index {0} with Value {1} ", iCount - 1, iCount);
 iNumber[iCount - 1] = iCount;
 }
 Console.WriteLine("Array Item initialised successfully");
 //Accessing Array item
 Console.WriteLine("Accessing Array item using Index");
 /**Since Array item should starts from 0 so we have initialise iCount to 0 to access the value at Zero index.
 * The 10th value of the Array will be at 9th index so our condition should be less the 10
 * */

for (int iCount = 0; iCount < 10; iCount++)
 {
 Console.WriteLine("Access Array Item at {0} with value {1} ", iCount, iNumber[iCount]);
 }

You can also access the array item using the foreach loop. Here is the code for this:

 foreach (int iValue in iNumber)
 {
 Console.WriteLine("Array item using foreach loop {0}", iValue);
 }

Looking for the complete code for the example here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayDemo
{
 class Program
 {
 static void Main(string[] args)
 {
 int[] iNumber=new int[10];
 //intialize Array item
 Console.WriteLine("Storing Values to Array");
 for (int iCount = 1; iCount <= 10; iCount++)
 {
 //Since array index starts from 0 so we have subtract 1 from iCount
 Console.WriteLine("Storing Value at Index {0} with Value {1} ", iCount - 1, iCount);
 iNumber[iCount - 1] = iCount;
 }
 Console.WriteLine("Array Item initialisedsuccessfully");
 //Accessing Array item
 Console.WriteLine("Accessing Array item using Index");
 /**Since Array item should starts from 0 so we have initialise iCount to 0 to access the value at Zero index.
 * The 10th value of the Array will be at 9th index so our condition should be less the 10
 * */

for (int iCount = 0; iCount < 10; iCount++)
 {
 Console.WriteLine("Access Array Item at {0} with value {1} ", iCount, iNumber[iCount]);
 }
 //Accessing Array item using foreach loop
 Console.WriteLine("Accessing Array item using foreach loop");
 foreach (int iValue in iNumber)
 {
 Console.WriteLine("Array item using foreach loop {0}", iValue);
 }
 Console.ReadKey();


 }
 }
}

Run the application and verify the output:

Array

You can download the complete source code for the application from here.

Hope you understand how to declare, initialize an Array in C#. We will discuss more on Array in later sessions.

Read More:

Thank You.

 

Leave a Comment