2014-07-24 3 views
0
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 
int oddNumbers = numbers.Count(n => n % 2 == 1); 
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6); 
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index); 

의 요소의 색인은 이들 I 배열 "숫자"의 요소로서 N을 고려하여 잘 처음 두 람다 표현식을 이해 http://msdn.microsoft.com/en-us/library/bb397687.aspx람다 식 : 배열

찍은 C# 코드이다.

그러나 세 번째 람다 식은 실제로 "색인"과 혼동합니다. (n, index) 배열에 대해 설정된 람다 매개 변수 중 하나입니까? 이것은 관례입니까?

컬렉션 위에 TakeWhile 반복
+0

인덱스 meens 지수,이 표현은 숫자 어레이에있는 모든 요소를 ​​반환 ? – Farrokh

+0

그것은 단지'TakeWhile()'이 받아들이는 델리게이트의 일부입니다. 여기에서 읽을 수 있습니다 : http://msdn.microsoft.com/en-us/library/vstudio/bb548775%28v=vs.100%29.aspx. * 술어 * 매개 변수를 확인하십시오. – itsme86

+0

특정 방법의 문서를 보았을 때 무엇을 발견했으며, 그 중 어떤 부분을 혼동스럽게 생각하니? – Servy

답변

1

경우 :

:

n가 요소
index된다 소자

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 
// As TakeWhile iterates over the array: 
// "n" is the value of the element 
// "index" is the index of the element 
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index); 
foreach(var n in firstSmallNumbers) 
    Console.WriteLine(n); 

출력 인덱스

5
4

실행 이것 : 숫자 값이 그 position.OK 미만인 만날 때까지 배열 요소 https://dotnetfiddle.net/4NXRkg