SEARCH C# PROGRAMMING CODES HERE

Array in C#

Array
In C# variables hold a single value of the same data type, for example, int y=20;. So what if, when we want to hold more than one value of the same data type than we can use an array to perform this task. An array is a collection of like-composed variables that are alluded to by a typical name. Also, every data items are called elements of the array. The information sorts of the elements might be any data type like char, int, float and so on. And the elements are put away in a touching area. Length of the array determines the number of elements present in the array.


Array Declaration

Syntax 

< data_type > [ ] < name of the array >

Precedent : 

int[] x;















Explanation

< data_type > /* It is used to define the element type of the array.*/

[ ]  /* It is used to define the size of the array.*/

< name of the array >  /* define name of  an array. */

Array Initialization
Syntax :

data_type [ ] < name of  the array > = new < data_type > [size];

int[]x=new int[6];
/*defining new array size [6]*/



Accessing Array Elements
Syntax :
data_type [ ] < name of the array > = new < data_type > [size];
< array_name > [size]=<number of elements> ;

int[] x = new int[6];

x[0] = 10;
x[1] = 45;
x[2] = 60;
x[3] = 74;
x[4] = 50;
x[5] = 90;


Array values(elements) are put away consecutively in the memory and that is the reason it performs quicker.

This example demonstrates accessing array elements using for Loop

using System;

namespace accessing_array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] x = new int[6] { 10, 45, 60, 74, 50, 90 };

            for (int i = 0; i < x.Length; i++)
                Console.WriteLine(x[i]);
                Console.ReadKey();
        }
        
    }
}  

There are the following couple of significant ideas identified with an array which ought to be obvious to C# software programmers.

1-Multidimensional arrays
C# underpins multidimensional arrays. The least complex type of the multidimensional array is the two-dimensional array.

Initializing Multidimensional array.


 int[,] x = new int[6, 2] 

{
{ 0,10 },   /* initializing for row indexed by 0 */
{ 1,45 },  /* initializing for row indexed by 1*/
{ 2,60 },  /* initializing for row indexed by 2*/
{ 3, 74},  /*  initializing for row indexed by 3 */
{ 4,50 },  /* initializing for row indexed by 4 */
{ 5,90 }   /*  initializing for row indexed by 5*/
};

Accessing Multi-Dimensional Array Elements

Program

using System;

namespace Acces_Multi_Dimensional
{
    class Program
    {
        static void Main(string[] args)
        {
         /*  an array with 6 rows and 2 columns */
            int[,] x = new int[6, 2] { { 0,10 }, { 1,45 }, { 2,60 }, { 3, 74 }, { 4,50 }, { 5,90 } };
            int i, j;

   /* display each array elements */
            for (i = 0; i < 6; i++)
            {

                for (j = 0; j < 2; j++)
                {
                    Console.WriteLine("x[{0},{1}] = {2}", i, j, x[i, j]);
                }
            }
            Console.ReadKey();

        }
    }
}





















2-Jagged arrays 
C# underpins multidimensional arrays, which are varieties of arrays.
So let's understand through an example below.
 /* Jagged arrays Declaration of two elements */
            int[][] x= new int[2][];

            /*Initialization of  the elements */ 

            x[0] = new int[5] { 1, 2, 3, 4, 5 };
            x[1] = new int[6] { 10, 45, 60, 74, 50,90 };
So let's create a program to display array elements using for loop as shown below.
Program
using System;

namespace Jagged_arrays 

{
    class Program
    {
        static void Main(string[] args)
        {

            /*Declaration of two elements*/

            int[][] x= new int[2][];

            /* Initialization of  the elements */ 

            x[0] = new int[5] { 1, 2, 3, 4, 5 };
            x[1] = new int[6] { 10, 45, 60, 74, 50,90 };
           
            /* To Display array elements*/
            for (int i = 0; i < x.Length; i++)
            {
                System.Console.Write("Row [" + i + "] Elements ");
                for (int y = 0; y < x[i].Length; y++)
                    Console.Write(x[i][y] + " ");
                    Console.WriteLine();
            }           
                    Console.ReadKey(); 
            } 
           

        }

    }















Array Class Properties 

The array has a fixed memory, and it uses 32-bit integer to represents the total elements in all the dimensions of the Array, and also 64-bit integer to represents the total elements in all the dimensions of the Array.


No comments:

Post a Comment