[英]From where arguments are passed to parameters of a lambda expression that is used within PLINQ?
我正在使用下面的C#代码:
//Custom structure
struct IndexedWord
{
public string Word;
public int Index;
}
static void Main(string[] args)
{
string[] wordsToTest = {"word1", "word2"};
var query = wordsToTest
.AsParallel()
.Select((word, index) =>
new IndexedWord {Word = word, Index = index});
foreach(var structs in query)
{
Console.WriteLine("{0},{1}", structs.Word,structs.Index);
}
Console.WriteLine();
Console.ReadKey();
}
//输出word1,0 word2,1
问题:上面的代码工作正常。 执行代码时,“选择”查询运算符中的lamba表达式将返回自定义结构“ IndexedWord”的实例。 表达式的参数从wordToTest []数组接收参数值。 例如,如果将参数“ word”传递给值“ word1”,则将参数“ index”传递给wordToTest []数组中“ word1”的对应索引位置。 我无法确切地知道在查询的哪一点(可能在内部)发生这种提取和将参数传递给lambda表达式的情况。 如何提取wordsToTest []数组的数据及其索引位置并将其传递给lamba表达式的参数? 是什么原因导致提取? 请对此澄清。 谢谢。
您是否听说过C#中的并行编程? 只是查询而已。 该查询与main方法并行进行。
“选择”方法是一种从源数组wordToTest []中提取每个数据值及其各自的索引值的方法。
函数调用:
wordsToTest.Select((word, index) =>
new IndexedWord { Word = word, Index = index });
调用构造函数:
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, TResult> selector)
上面提到的Select()方法属于Enumerable类。 有关更多详细信息,请参阅下面提到的链接: https : //msdn.microsoft.com/zh-cn/library/bb534869(v=vs.110).aspx
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.