[英]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.