C#数组传递函数如C++?

etl*_*lds 3 c#

有没有什么办法可以C#在像C++这样的函数中传递一个数组?

在C++中,我们可以传递数组加偏移量和长度非常整齐,如下所示

void myFunction(int A[], int m) // m is length of A
{
    myFunction2(A+1, m - 1); 
}
Run Code Online (Sandbox Code Playgroud)

C#也可以吗?

mel*_*okb 6

当然.但是,您无法修改指针,因此您需要单独传递偏移量.此外,您不需要传递长度,因为所有C#Arrays都具有.Length获取数组大小的属性.

void MyFunction(int[] A, int offset)
{
    MyFunction2(A, offset + 1);
}
Run Code Online (Sandbox Code Playgroud)