Array in C#

Rumman Ansari   Software Engineer   2019-07-15   6363 Share
☰ Table of Contents

Table of Content:


Array

  • Array is used to store multiple values of the same type in a variable.
  • It is used to store values of value types and reference types.
  • Size of the array should be decided at the time of creation itself.

Syntax

dataType[] varName=new dataType[size];

Example

int[] marks=new int[10];
  • marks is an integer array which can hold 10 integer values.
  • Array store from index 0 till size-1 values. 'marks' array can hold
    values from marks[0] to marks[9].

Sample which stores and retrieve the marks of 5 subjects

 static void Main(string[] args)
{
int[] marks = new int[5];
Console.WriteLine("Enter marks of 5 subjects");
for (int i = 0; i < 5; i++)
{
marks[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Marks are as follows: ");
for (int i = 0; i < 5; i++)
{
Console.WriteLine(marks[i]);
}
Console.ReadKey();
}

Output

Enter marks of 5 subjects
78
89
67
98
72
Marks are as follows:
78
89
67
98
72

In case we have to store a set of employee details then we can use an array of type class. So in the following sample, for each employee, we have to store name, id and performance pay. So first we create an employee array with the required attributes and a constructor is written to assign the values and there are properties used to retrieve the values.
In our main program, the employee array is created and values are stored and finally retrieved. The size of employee array is 100. So it can store a maximum of 100 employees.

//In the following line and Array of type employee is created
Employee[] empList = new Employee[100];
//In the following line the object of type employee is created by passing the
required values and storing it in array.
empList[i] = new Employee(name, id, performancePay);
//In the following line the Id of employee is accessed from array.
Console.WriteLine("Id is "+empList[i].Id);

Sample

class Employee
{
string name;
int id;
int performancePay;
public Employee(string name, int id, int performancePay)
{
this.name = name;
this.id = id;
this.performancePay = performancePay;
}
public string Name
{
get
{
return (name);
}
set
{
name = value.ToUpper();
}
}
public int PerformancePay
{
get
{
return (performancePay);
}
set
{
performancePay = value;
}
}
public int Id
{
get
{
return (id);
}
set
{
id = value;
}
}
class Program
{
static void Main(string[] args)
{
//Array of type employee is created
Employee[] empList = new Employee[100];
string name;
int id;
int performancePay;
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Enter name,id,performance pay");
name=Console.ReadLine();
id=Convert.ToInt32(Console.ReadLine());
performancePay=Convert.ToInt32(Console.ReadLine());
//i th employee object is created
empList[i] = new Employee(name, id, performancePay);
}
//View the details of all the employees
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Details of Employee no :"+ i+1);
Console.WriteLine("Id is "+empList[i].Id);
Console.WriteLine("Name is "+empList[i].Name);
Console.WriteLine("Performance Pay is
"+empList[i].PerformancePay);
}
Console.ReadKey();
}
}