0

I am currently messing around with C# in order to be able to approach ASP.NET with an acceptable knowledge base. In order to do so I occasionally look at my old C++ code and see if I can figure out how to make it work in C#.

So I figured that I will look into arrays, a pretty common way to store data. What I was wondering is what would the C# equivalent of creating a two dimensional array via pointers would look like?

I know that in C++ one way to do it could look like this:

double **myArray; 

myArray = new double * [rows];

for(int I = 0; I < rows; I++) {
myArray[I] = new double[cols];
}

I also know that in C# you could easily just say double [rows,cols] and that would create a 2D array. I know that C++ and C# most likely treat pointers differently, but just for the sake of interest, how would I mimic a similar behavior in C#?

Cheers.

SomeStudent
  • 2,856
  • 1
  • 22
  • 36
  • 1
    The rule of thumb in C# is to never use pointers unless you have a very good reason for doing so. What is it you are trying to accomplish with this? – Abion47 Dec 12 '16 at 04:35
  • Not sure why the -1, oh well. Trying to accomplish really nothing aside from furthering my knowledge. Knowledge for knowledge sake. On occasion I like to try to convert my old C++, or at least try, into C# code. So I was wondering if that were possible. – SomeStudent Dec 12 '16 at 04:47

2 Answers2

0

C# arrays and C++ pointers are incredibly similar, with the biggest difference is that C# has a layer of managed logic on top of the array data.

An array of pointers in C# is possible. You could do it, for example, with:

unsafe
{
    double*[] ptrArray = new double*[5];
    ptrArray[2][0] = 4.0;

    double*[,] ptr2DArray = new double*[5,5];
    ptr2DArray [2, 2][0] = 4.0;
}

However, like I said in my previous comment, using pointers in C# is discouraged, and generally you want to use a managed approach whenever possible. Personally, I cannot fathom a need for creating an array of pointers (much less a 2D array) and I imagine that if someone's program needs a structure like this, then they probably need to rethink their approach.

Abion47
  • 22,211
  • 4
  • 65
  • 88
0

Usually pointers are very less used in C#, When you use pointers in C# it is considered as unsafe context.

Please check following MSDN Pointer Document that illustrates how to use pointers.

I would like to say don't use pointers if not required. You can use Ref Keyword and Out keyword to pass your parameter as a reference.

When you use pointer you loose all managed code benefits.

You can check C# ref is it like a pointer in C/C++.

Apart from that i think that all arrays are already Reference Type even if it contains all members of value type.

Check Value Types and Reference Types in C#.

I hope it helps.

Community
  • 1
  • 1
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
  • Out of curiosity, is there a significant difference between & and ref when passing items by reference? – SomeStudent Dec 12 '16 at 05:14
  • @SomeStudent There are some subtle differences that have to do with how types and objects are handled and passed around in the CLR, but in practice, not really. – Abion47 Dec 12 '16 at 05:17