Skip to main content

C# - Arrays

Introduction

If we create a variable of type int also assign 10 as value in it. If we want to store more than one value in it then its not possible. That why array comes to the picture, If you want to store similar type and more then one value into it then should take array.
Array Definition 
"An array is a collection of similar data type or you can say a single variable hold multiple other variables, those variable known as label of array. Lets take an simple example, suppose you have two things, which are ball and bat and you want to store it, use a box. Put the both things in separate boxes, also with label. Here 'array' is a box and label is 'variable name'.
Basically array consists of two things first one is index and second one is value, If you want to access specific value in an array, use index. When we insert items into an array, default index started from 0. You can insert items randomly at any index position. Lets take an example for clarification that how to insert item into an array. Before learn this example, first should take array declaration.

Syntax of declaring array


datatype [] identifier;

where, you can use any data type to declare an array like int , float, double, string etc. This bracket [] known as rank of the array, its doesn't take decimal type value , always take integer. The value in the rank known as size of the array. Suppose i take 10 in the rank, then array index start from 0 to 9. Here 0 is the starting index and 9 is the last index. All arrays value consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Lets more about an array that is how to initialize array variable. 
Array initialization means you can assign some values in to the array. 
Before initialization, should take new keyword for create instance of the array because array is a reference type. For example

int[] employee_age = new int[10];

In above mentioned example array hold 10 elements, means size of the array is 10. If you want to assign value in it, you can use index number. Like

employee_age[0] = 20;
employee_age[1] = 40;

Also You can assign values to the array at the time of declaration, like:

int [] employee_age = {20,21,22,23,24};
You can also create and initialize an array, like:
int [] employee_age = new int[5]  {20,21,22,23,24};
In the preceding example you can change the size of element, directly you can say re-sizing of an array. Also you can copy one array to another array in c#. In that case both array would locate to same memory location. like takes an simple example

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

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

            int[] employee_age = new int[5]{20,21,22,23,24};
            int[] second_array = employee_age;
            int i = 0;
            foreach (var item in second_array)
            {
                Console.WriteLine("arrays elements are[{0}]={1}",i,item);
                i++;
            }
            Console.ReadKey();
        }
    }
}

Code generate the following output
How to copy of one array element to another array element
Above mentioned example covers declaring of an array, initialization of array and how to access array element in c#.
In c#, when we create a new array in the memory then compiler automatically initialize all index value with 0. Take a look.

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

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

            int[] employee_age = new int[5];
            int[] second_array = employee_age;
            int i = 0;
            foreach (var item in second_array)
            {
                Console.WriteLine("arrays elements are[{0}]={1}",i,item);
                i++;
            }
            Console.ReadKey();
        }
    }
}
Code generate the following output
c# array initialize with default value 0

Comments

Popular posts from this blog

difference between structure and union in C Language

In c language article we will see the difference between union and structure. Both are the user define datatype in c language. See the table which is mentioned below: ASP.NET Video Tutorial Series Structure Union1.The keywordstruct is used to define a structure 1. The keyword union is used to define a union. 2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum ofsizes of its members. The smaller members may end with unused slack bytes. 2. When a variable is associated with a union, thecompiler allocates thememory by considering the size of the largest memory. So, size of union is equal to the size of largest member. 3. Each member within a structure is assigned unique storage area of location. 3. Memory allocated is shared by individual members of union. 4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset v…

Difference between Linear search and Binary Search in c language

SQL Video Channel : Download all SQL Video



Binary Search Linear Search Works only on sorted items. such as  1,2,3,4,5,6  etc
Works on sorted as well as unsorted items. 12,4,5,3,2,1 etc Very efficient if the items are sorted Very efficient if the items are less and present in the beginning of the list. such as Suppose your list items are : 12,3,4,5,1 and you want to search 12 number then you get beginning in the list. Works well with arrays and not on linked lists. Works with arrays and linked lists.
Number of comparisons are less More number of comparisons are required if the items are present in the later part of the array or its elements are more.

Memory representation of Linked List Data Structures in C Language

Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node …