Pointers in C# to make int array?

Posted by Joshua on Stack Overflow See other posts from Stack Overflow or by Joshua
Published on 2010-03-30T16:03:57Z Indexed on 2010/03/30 16:13 UTC
Read the original article Hit count: 330

Filed under:
|
|
|

The following C++ program compiles and runs as expected:

#include <stdio.h>

int main(int argc, char* argv[])
{
    int* test = new int[10];

    for (int i = 0; i < 10; i++)
            test[i] = i * 10;

    printf("%d \n", test[5]); // 50
    printf("%d \n", 5[test]); // 50

    return getchar();
}

The closest C# simple example I could make for this question is:

using System;

class Program
{
    unsafe static int Main(string[] args)
    {
        // error CS0029: Cannot implicitly convert type 'int[]' to 'int*'
        int* test = new int[10];

        for (int i = 0; i < 10; i++)
            test[i] = i * 10;

        Console.WriteLine(test[5]); // 50
        Console.WriteLine(5[test]); // Error

        return (int)Console.ReadKey().Key;
    }
}

So how do I make the pointer?

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++