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
Example
- 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
- static void Main()
- {
- unsafe
- {
- int[] my = new int[3]
- {
- 10, 20, 30
- };
- int * s = stackalloc int[3];
- s[0] = my[0];
- s[1] = my[1]; * (s + 2) = my[2];
- int * ppp = & s[1];
- System.Console.WriteLine("The size of integer datatype : {0}", sizeof(int));
- System.Console.WriteLine("The address stored of array in s pointer of base element : {0}", (int) s);
- System.Console.WriteLine("The address stored of array in ppp pointer of second element : {0}", (int) ppp);
- System.Console.WriteLine("The address stored of array element s[0]: {0}", (int) s[0]);
- System.Console.WriteLine("The address stored of array element s[1]: {0}", (int) s[1]);
- System.Console.WriteLine("The address stored of array element s[3]: {0}", (int) s[2]);
- }
- System.Console.ReadKey();
- }
Running above program will give an error of type unsafe as:
To resolve this error right click on our solution folder and go to properties window as:
Go to build option & click on
Allow unsafe code checkbox to allow running unsafe code.
After compiling output of our code is:
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?
- int[] my = new int[3] { 10, 20, 30 };
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:
- s[0] = my[0];
- s[1] = my[1];
- *(s + 2) = my[2];
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.