C # is not C ++ - don't expect the same things to work in C # that worked in C ++. This is a different language, with some inspiration in the syntax.
In C ++, accessing an array is a short hand for manipulating a pointer. This is why the same thing:
test[5] *(test+5) *(5+test) 5[test]
However, this is not the case in C #. 5[test] not valid C # because System.Int32 does not have an indexer property.
In C #, you very rarely want to deal with pointers. You better just consider it as an int array directly:
int[] test = new int[10];
If you really want to deal with pointer math for some reason, you need to specify your unsafe method and put it in a fixed context . This would not be typical of C #, and in fact it is probably something completely unnecessary.
If you really want to do this job, the closest you can do in C #:
using System; class Program { unsafe static int Main(string[] args) { fixed (int* test = new int[10]) { for (int i = 0; i < 10; i++) test[i] = i * 10; Console.WriteLine(test[5]);
(Again, this is really weird C # - not what I would recommend ...)
Reed copsey
source share