본문 바로가기

Programming/C#

C# Func vs Action vs Predicate

반응형

Func

  • delegate
  • return value
  • linq 예시
var ids = list.Select(d => d.Id);

 

Action

  • delegate
  • linq 예시
list.ForEach(d => { d.SomeProperty + 1; });

 

Predicate

  • Func<T, bool>
public delegate bool Predicate<in T>(T obj); 

ref : https://github.com/microsoft/referencesource/blob/master/mscorlib/system/action.cs

 

microsoft/referencesource

Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework - microsoft/referencesource

github.com

 

요약

Action은 특정 기능 수행할 때

Func은 특정 기능 수행과 함께 값을 리턴 받을 때

Predicate는 Func의 특수 케이스(리턴 타입이 bool인)

 

 

반응형