How to access array elements using a pointer in C#?

In C#, an array name and a pointer to a data type same as the array data, are not the same variable type. For example, int *p and int[] p, are not the same type. You can increment the pointer variable p because it is not fixed in memory but an array address is fixed in memory, and you can't increment that.

Here is an example −


using System;

namespace UnsafeCodeApplication {
   class TestPointer {
      public unsafe static void Main() {
         int[] list = {5, 25};
         fixed(int *ptr = list)

         /* let us have array address in pointer */
         for ( int i = 0; i < 2; i++) {
            Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr + i));
            Console.WriteLine("Value of list[{0}]={1}", i, *(ptr + i));


Here is the output −

Address of list[0] = 31627168
Value of list[0] = 5
Address of list[1] = 31627172
Value of list[1] = 25
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know