Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数组从C#.Net中的特定索引传递到函数_C#_.net_Arrays - Fatal编程技术网

将数组从C#.Net中的特定索引传递到函数

将数组从C#.Net中的特定索引传递到函数,c#,.net,arrays,C#,.net,Arrays,我不熟悉C#(以前在C++上工作),我过去常常将数组传递给具有特定索引的函数。下面是C++中的代码, void MyFunc(int* arr) { /*Do something*/ } //In other function int myArray[10]; MyFunc(&myArray[2]); 我可以在C#Net*中执行类似操作吗?* 请参阅此处以了解有关的更多信息 由于数组是可枚举的,所以可以使用 linq版本是我的首选。然而,它将被证明是非常低效的 你可以 int myA

我不熟悉C#(以前在C++上工作),我过去常常将数组传递给具有特定索引的函数。下面是C++中的代码,

void MyFunc(int* arr) { /*Do something*/ }

//In other function
int myArray[10];
MyFunc(&myArray[2]);
我可以在C#Net*中执行类似操作吗?*


请参阅此处以了解有关的更多信息

由于数组是可枚举的,所以可以使用


linq版本是我的首选。然而,它将被证明是非常低效的

你可以

int myArray[10];
int mySlice[8];
Array.Copy(myArray, 2, mySlice, 0);
并将mySlice传递给函数

。NET具有解决此确切用例的结构


但我从未在代码中实际看到过这种结构,理由很充分:它不工作。值得注意的是,它没有实现任何接口,例如
IEnumerable
。因此,Linq解决方案(=使用
跳过
)是最好的选择。

最简单的方法可能是:

public void MyFunction(ref int[] data,int index)
    {
        data[index]=10;
    }
这样称呼它:

int[] array= { 1, 2, 3, 4, 5 };
Myfunction(ref array,2);
foreach(int num in array)
    Console.WriteLine(num);

这将打印1,2,10,4,5

他希望传递数组的一部分,而不是全部。这是完全不同的。我认为不可能做这样的事情。它起作用了,我不必在skip语句之后添加数组。myArray.Skip(1).ToArray()@vrajs5:如果MyFunc一个接一个地处理数组元素,请考虑使MyFunc接受IEnumerable。ToArray()创建myArray的副本。啊,太酷了,我以前从未见过Linq Skip函数。但就我个人而言,这似乎效率太低了,我想我应该将MyFunc更改为在内部跳过前2个元素,或者甚至为MyFunc的元素编号添加一个新的参数来开始。。。我想这取决于。。。
public void MyFunction(ref int[] data,int index)
    {
        data[index]=10;
    }
int[] array= { 1, 2, 3, 4, 5 };
Myfunction(ref array,2);
foreach(int num in array)
    Console.WriteLine(num);