Skip to main content

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.


Comments

Popular posts from this blog

Media Player in C# Visual Studio By Rohit Programming Zone

Media Player  in C# Visual Studio   Source Code ----------------------------------------------------------------------------------------------------------------------------- using System; using System.Windows.Forms;   namespace Media_Player_in_C_Sharp_By_Rohit_Programming_Zone {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         string [] paths, files;           private void btn_open_Click( object sender, EventArgs e)         {             OpenFileDialog ofd = new OpenFileDialog ();   ...

ImageEditor in C# Visual Studio 2017

ImageEditor in C # Start a new project in Visual Studio > choose Visual C# then click on Windows Desktop then select WindowsForm App(.Net Framework)  and name it as ImageEditor then press ok. First, go to the Form1 properties  S ection and set the Size as following. Form1-size=952, 607 Now Add 3 panels from the toolbox and set the properties as follows. After setting the properties it will look as follows. Now Add 2-picture box from the toolbox and go to the properties and set the properties as follows. Now add 15-Button from the toolbox. Now add 3-textbox from the toolbox. Now add 3-trackbar and 5-label from the toolbox. Now go to the properties section and check all the Components are named correctly or not. Once  all the Components are named correctly then  move to the coding part. (Now the Coding Part) Now go to the Form1 code view and  ...

Microsoft tools Download

1-Visual Studio Code Visual Studio Code is a  fast and free  source code lite editor that can be used with the various programming languages. Download 2-Visual Studio 2019 Community Download 3-Microsoft Visual Studio 2010 Microsoft Visual Studio 2010 Development environment specialized in Windows systems. Download 4-VISUAL STUDIO 2013 EXPRESS FOR WEB Visual Studio Express 2013 for Web provides the core tools for creating compelling, innovative web applications and services. This includes the ASP.NET editors for HTML5, CSS3, JavaScript, etc. Download 5-Piskel Download 6-Pichon Download 7-Microsoft XNA Framework Redistributable 4.0 Description The XNA Framework has all the necessary runtime components to execute a game, which was developed using Microsoft XNA Game Studio 4.0. Version 4.0 of the framework contains improved functionality and new features.