List<T>.ForEach(Action<T>) 메서드

정의

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
Action<T>

Action<T>의 각 요소에 대해 수행할 List<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> 에서 기본 컬렉션을 수정하는 것은 지원되지 않으며 정의되지 않은 동작을 발생합니다.

적용 대상

추가 정보