Sunday 10 August 2014

Arrays in C#

Arrays:
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.


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

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
Arrays c = new Arrays ();
c. array ();
              Console.ReadLine();
        }

    }
}
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class Arrays
    {
        public void array()
        {
            //using for loop
            int[] n = new int[10];
            int i, j;
            for (i = 0; i < 10; i++)
            {
                n[i] = i + 100;
            }
            for (j = 0; j < 10; j++)
            {
                Console.WriteLine("the element of array are {0}={1}", j, n[j]);
            }

            //using foreach loop
            foreach (int a in n)
            {
                int c = a - 100;
                Console.WriteLine("elements are {0}={1}", c, a);
                c++;
            }
            Console.ReadKey();
        }
    }
}

Output:

No comments:

Post a Comment