C#:以 IEnumerable<Type> 作为参数的方法。什么是有效输入?

C# : Method having IEnumerable<Type> as argument. What is a valid input?

我正在尝试为我的方法 Pairwise 定义一个有效的输入。 Pairwise 接受一个 IEnumerable 参数,我很难弄清楚到底是什么。我已经尝试了很多东西,但永远无法真正到达那里。

public delegate void PairwiseDel(Type left, Type right);

public static void Pairwise(IEnumerable<Type> col, PairwiseDel del)
{
   // stuff happens here which passes pairs from col to del
}

有人可以告诉并说明我的方法的有效输入是什么吗?

这将是一个有效的输入:

var collection = new List<Type>();
collection.Add(typeof(string));
collection.Add(typeof(int));

PairWise(collection, YourDelegateHere);

IEnumerable<T> 是 .NET 库中一个非常重要的接口。它表示描述类型 T.

元素序列的抽象

这个通用接口有多个实现:

  • 内置一维数组T[]实现IEnumerable<T>
  • 所有通用 .NET 集合实现 IEnumerable<T>
  • 使用 yield return 的方法产生 IEnumerable<T>
  • .NET LINQ 库中的多个方法都采用和 return IEnumerable<T>

如果您想测试您的方法,请将数组传递给它 Type[]:

var items = new Type[] { typeof(int), typeof(string), typeof(long) };
Pairwise(items, (a, b) => {
    Console.WriteLine("A={0}, B={1}", a.Name, b.Name);
});