Arrays in C#

What is an Array?

Arrays are the derived data type in the C# programming language which can store the primitive type of data such as int, char, double, float, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C# array is beneficial if you have to store similar elements.

Example

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

namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[2]; //int array initialize
            int sum = 0;  
            for (int i = 0; i < 2; i++)  //for loop working 2 times
            {
                Console.WriteLine("Enter your number");  
               arr[i]=int.Parse(Console.ReadLine());  //read user integer input
            }
            for (int i = 0; i < 2; i++)
            {
                sum = sum + arr[i];  //adding the sum of array indexes
            }
            Console.WriteLine("Sum = "+ sum);  //show the result
            Console.ReadLine();
        }
    }
}

Output

Enter your number
10
Enter your number
20
Sum = 30


Write a comment


Name Email Message