Arrays In C#

Introduction

In this article we will discuss about arrays in C# programming, how can we get address of each element in an array.

Arrays in C#

  • Array is a collection of similar data types.
  • The base class for arrays in C# is the System.Array class.
  • Array indexing starts from always 0. We can access element by specifying index number in curly braces.
  • Base address of array is stored on stack & elements are kept on heap.

Syntax for Array

    Data_Type[] Array_Name = new Data_Type[number_of_elements];

Example

  1. int[] my_Array = new int[5] { 1, 3, 5, 7, 9 };  

 

  • For the index 0 (my_ Array[0]) will get first element i.e. 1.
  • For the index 1 (my_ Array[1]) will get first element i.e. 3 and so on.

Important: Array is internally stored as pointers. For example, daily we use Mobile phones. To search any contact in our mobile we use search option where we placed a name as a string / character to access particular number.

So this uses pointes internally. Pointers have a size of 4 bytes if we store an integer variable.

Example

  1. static void Main()  
  2. {  
  3.     unsafe  
  4.     {  
  5.         int[] my = new int[3]  
  6.         {  
  7.             10, 20, 30  
  8.         };  
  9.         int * s = stackalloc int[3];  
  10.         s[0] = my[0];  
  11.         s[1] = my[1]; * (s + 2) = my[2];  
  12.         int * ppp = & s[1];  
  13.         System.Console.WriteLine("The size of integer datatype : {0}"sizeof(int));  
  14.         System.Console.WriteLine("The address stored of array in s pointer of base element : {0}", (int) s);  
  15.         System.Console.WriteLine("The address stored of array in ppp pointer of second element : {0}", (int) ppp);  
  16.         System.Console.WriteLine("The address stored of array element s[0]: {0}", (int) s[0]);  
  17.         System.Console.WriteLine("The address stored of array element s[1]: {0}", (int) s[1]);  
  18.         System.Console.WriteLine("The address stored of array element s[3]: {0}", (int) s[2]);  
  19.     }  
  20.     System.Console.ReadKey();  
  21. }  
Running above program will give an error of type unsafe as:

error

To resolve this error right click on our solution folder and go to properties window as:

properties

Go to build option & click on Allow unsafe code checkbox to allow running unsafe code.

build

After compiling output of our code is: 

output

Here we can see size of integer data type, every element in array having same size as its data types, so first element is at 103934088, second element is at 103934092. The difference is also 4 bytes.

See above example, I have my array names which is having 3 elements of type integer data types. Then I have created one pointer which will create array on stack with 3 elements. I have created stack based because further we have to find address of each element.

Diagram

How array elements get stored?
  1. int[] my = new int[3] { 10, 20, 30 };  
elements

In the above diagram addresses are considered as per our assumption.

Now concentrate on the following elements.

If we want to access my[2] element, then we know this has stored 30 values inside it. Also, we can access other elements using pointer and many ways as:
  1. s[0] = my[0];  
  2. s[1] = my[1];  
  3. *(s + 2) = my[2];  
array

Summary

This article will help fresher candidates to understand array in a simple way. Hope you enjoyed this. Don’t forget to comment on the article.

 


Recommended Free Ebook
Similar Articles