Home > DeveloperSection > Articles > Single Dimension Array In C#.NET

Single Dimension Array In C#.NET


.NET .NET 
Ratings:
0 Comment(s)
 13353  View(s)
Rate this:

Single Dimension Array In C#.NET

Whenever you want to store values in a single row then we follow the concept of single dimension array. In a single dimension array we need only one index to access the member of array.The one dimensional array or single dimensional array in C# is the simplest type of array that contains only one row for storing data. It has single set of square bracket (“[]”). 

Syntax

<data type> [ ] <array Name>=new <data type>[<length>];

Where  

 <data type> represents’ data types such as int, float, double, string or any other reference type.

[ ] symbol is used to create a single dimension array.

 

<array name> represents identifier through which we identify and use array.

new is a keyword which is used to allocate memory to the array.

[<length>] represents size of the array.

Note:

Ø  As we know that array have a fixed size that means when we create an array then we have to tell that what is the size of the array.

Ø  Arrays are 0(zero) based index. That means first elements of arrays is always start with zero index.

 

Example for Declaring Single Dimension Array

Ø  int [] num=new int[5];  //An integer types array is declared which have length of five elements.

Ø  float [] salary=new float[8]; //An float types array is declared which have a length if eight elements.

Ø  char [] ch=new char[5]; //An character type of array is declared.

Ø  string [] name=new string[10]; //A string type of array is declared.

 

Inserting Values in Array

After declaring arrays of single dimension we have to insert some values in array. For inserting values in array we have to access the index of array. Each index is start by 0 and goes to less than length of array. Means to say if an array of length 5 is created then we can insert total 5 elements in array. First element is in 0 index and last element is 5-1=4 index.

 Syntax for Inserting Values in Array

<array name>[<index number>]=<value>

Where

Ø  <array name> represents the name of the array in which array value needs to be inserted.

Ø  <index number> represents in which index value should be inserted.

Ø  <value> represents possible value that should be stored in appropriate index.

 

Example for Inserting Values in Array

Ø  num[0]=45; //value 45 is stored in index 0 of num array.

Ø  ch [3]=’C’; //character C is stored in index 3 of char array.

Ø  salary[2]=45000.45f; //Salary45000.45 is stored in index 2 of salary array.

 

Using loops for inserting/traversing values in array

Whenever we want to insert continues value in an array then we use concept of loops. We can use any type of loops for inserting values in array but more commonly for loop is used for array purpose.

Inserting/Traversing of array by using for loops

Example 1: inserting of array by using for loop

void insertArray()

        {

            int[] num = new int[5]; //An array is declared with 5 variables

            //Length property of num array is used to calculate the size of the array.

            for (int i = 0; i < num.Length; i++)

            {

                Console.Write("Enter Any Value-->  "); //A message to console device is displayed.

                num[i] = Convert.ToInt32(Console.ReadLine()); //A value is stored on i th index of num array.

            }

        }

 

Example 2: Traversing of array by using for loop

 

void traverseArray(int[] num)

    {

        for (int i = 0; i < num.Length; i++)

        {

            Console.WriteLine("Array Value Is : {0}", num[i]);

        }

    }

 


Don't want to miss updates? Please click the below button!

Follow MindStick