Single dimensional arrays in C#

Syntax Single Dimensional Array

<data_type>[] variable_name = new <data_type>[SIZE]; 

Example

int[] X = new int[100]; 

Initialization for a single-dimension array

int[] X = {1,2,3,4,5}; 

Program

using System;

namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int[] X;

            X = new int[5];

            Console.Write("Enter Elements : \n");
            for (i = 0; i < 5; i++)
            {
                Console.Write("\tElement[" + i + "]: ");
                X[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.Write("\n\nElements are: \n");
            for (i = 0; i < 5; i++)
            {
                Console.WriteLine("\tElement[" + i + "]: "+X[i]);
            }      
        }
    }
}

Output

Output:
Enter Elements :      
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50
                      
Elements are:         
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50    
Press any key to continue . . .

Leave a Comment

error: Alert: Content is protected!!