小编ndn*_*ndn的帖子

混合 ref 和 params 关键字是不可能的吗?

我想要一个可以执行此操作的映射函数:

public static void Map<T>(IEnumerable<T> src, params T[] dst)
{
    for (int i = 0; i < dst.Length; i++)
        dst[i] = src.ElementAt(i);
}
Run Code Online (Sandbox Code Playgroud)

为了使这项工作正常进行, dst 还必须声明为 ref ,这似乎是不可能的。

这里它用于假想的单元测试:

int a = 0, b = 0, c = 0;
int[] arr = { 1, 2, 3 };
Tools.Map(arr, a, b, c);
Assert.AreEqual(a, 1);
Assert.AreEqual(b, 2);
Assert.AreEqual(c, 3);
Run Code Online (Sandbox Code Playgroud)

这有可能吗?它已经存在了吗?这是一个坏主意吗?

编辑:换句话说,我如何为该实现提供任意数量的参数:

public static void Map3<T>(IEnumerable<T> src, ref T a, ref T b, ref T c)
{
   a = src.ElementAt(0);
   b = src.ElementAt(1); …
Run Code Online (Sandbox Code Playgroud)

c# linq

3
推荐指数
1
解决办法
260
查看次数

标签 统计

c# ×1

linq ×1