Table of Contents

    Mastering Arrays in C#: A Comprehensive Guide

    Mastering Arrays in C#: A Comprehensive Guide

    What is Array in C#

    • 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

    If we need to store a set of employee details, we can use an array of type class. In the following example, for each employee, we store their name, ID, and performance pay. First, we create an employee array with the required attributes, and a constructor is defined to assign these values. Properties are used to retrieve the values.
    In the main program, the employee array is created, values are stored, and finally retrieved. The size of the employee array is set to 100, meaning it can store a maximum of 100 employees.

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

    Example

    class Employee
    {
        string name;
        int id;
        int performancePay;
    
        // Constructor to assign values
        public Employee(string name, int id, int performancePay)
        {
            this.name = name;
            this.id = id;
            this.performancePay = performancePay;
        }
    
        // Property for Name
        public string Name
        {
            get { return name; }
            set { name = value.ToUpper(); }
        }
    
        // Property for PerformancePay
        public int PerformancePay
        {
            get { return performancePay; }
            set { performancePay = value; }
        }
    
        // Property for ID
        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, performancePay;
    
            // Input employee details
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Enter name, ID, and performance pay:");
                name = Console.ReadLine();
                id = Convert.ToInt32(Console.ReadLine());
                performancePay = Convert.ToInt32(Console.ReadLine());
    
                // Create the i-th Employee object
                empList[i] = new Employee(name, id, performancePay);
            }
    
            // View the details of all employees
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Details of Employee no: " + (i + 1));
                Console.WriteLine("ID: " + empList[i].Id);
                Console.WriteLine("Name: " + empList[i].Name);
                Console.WriteLine("Performance Pay: " + empList[i].PerformancePay);
            }
    
            Console.ReadKey();
        }
    }