After announcement of array, you required to store value in array. In array you can store value at compile time by writing in the program or run time by taking input from the user. In this chapter, we will learn how to store value in array using c#.

Storing value directly in your C# program:

In your program, you can directly store value in array. You can store value in array by two ways.


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


   int[] numberArr = new int[5]
   numberArr[0] = 1;
   numberArr[1] = 2;
   numberArr[2] = 3;
   numberArr[3] = 4;
   numberArr[4] = 5;
But, if you are storing value in array by taking users input, then you can use loop to accept value in array. The following C# example will focus how to get input from users at runtime and store in an array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Store_Value_In_Array_Example
    class Program
        static void Main(string[] args)
            int i;
            int[] numberAarr = new int[5]; // 5 size array
            // Accepting value from user 
            for (i = 0; i < 5; i++)
                Console.Write("\nEnter your number:\t");
                //Storing value in an array
                numberArr[i] = Convert.ToInt32(Console.ReadLine());
            //Printing the value on console
            for (i = 0; i < 5; i++)
                Console.WriteLine("you entered {0}", numberAarr[i]);
In the above example we declared an array named numberArr. Then  using for loop, we traversed each index position of array and stored value.

Leave a Reply

Your email address will not be published. Required fields are marked *