Array indexing in C# (no pointers?)

  • Thread starter Thread starter MrAsm
  • Start date Start date
M

MrAsm

Moving from C++ to C#, it seems to me that there are no pointers in C#
to e.g. accessing array items.

So, should I just use integer indexes to "point" to array items?

e.g.

<C++>
double * pArray;

double * pX = pArray;
double * pZ = pArray + 2;
pZ++;
...

</C++>

int iX = 0; // pX
int iZ = 2; // pZ

array[iX]
array[iZ]


Thanks,
MrAsm
 
MrAsm,

That's pretty much the case. If you absolutely positively CAN'T live
without using pointers to manipulate arrays, you can use unsafe code, but I
would recommend against this, as there is no obvious benefit of using a
pointer here.

The only case I can see this being useful is when you want to work on a
subsection of an array and not have to do the arithmetic for the offset into
the original array every time. If you are using tons of these kinds of
operations, then I would say that unsafe code might be a good solution here.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top