List<T>.ForEach(Action<T>) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
의 각 요소 List<T>에 대해 지정된 작업을 수행합니다.
public:
void ForEach(Action<T> ^ action);
public void ForEach(Action<T> action);
member this.ForEach : Action<'T> -> unit
Public Sub ForEach (action As Action(Of T))
매개 변수
예외
action은 null입니다.
컬렉션의 요소가 수정되었습니다.
예제
다음 예제에서는 대리자를 사용하여 Action<T> 개체의 List<T> 내용을 인쇄하는 방법을 보여 줍니다. 이 예제 Print 에서는 이 메서드를 사용하여 목록의 내용을 콘솔에 표시합니다.
메모
C# 예제에서는 메서드를 사용하여 Print 콘텐츠를 표시하는 것 외에도 익명 메서드 를 사용하여 결과를 콘솔에 표시하는 방법을 보여 줍니다.
List<string> names = new List<string>();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard");
// Display the contents of the list using the Print method.
names.ForEach(Print);
// The following demonstrates the anonymous method feature of C#
// to display the contents of the list to the console.
names.ForEach(delegate(string name)
{
Console.WriteLine(name);
});
void Print(string s)
{
Console.WriteLine(s);
}
/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim names As New List(Of String)
names.Add("Bruce")
names.Add("Alfred")
names.Add("Tim")
names.Add("Richard")
' Display the contents of the list using the Print method.
names.ForEach(AddressOf Print)
End Sub
Shared Sub Print(ByVal s As String)
Console.WriteLine(s)
End Sub
End Class
' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard
설명
전달 Action<T> 된 개체에 대한 작업을 수행하는 메서드의 대리자입니다. 현재 List<T> 요소는 대리자에게 Action<T> 개별적으로 전달됩니다.
이 메서드는 O(n) 연산이며 여기서 n은 . 입니다 Count.
대리자의 본문 Action<T> 에서 기본 컬렉션을 수정하는 것은 지원되지 않으며 정의되지 않은 동작을 유발합니다.