C#
[C#] Linq Aggregate 사용하기
kjun.kr
2023. 9. 26. 12:49
728x90
시퀀스에 누적함수를 적용할때 사용됩니다.
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
// 문자열연결
var data = fruits.Aggregate((str1, str2) => str1 + ", " + str2);
Console.WriteLine(data);
// 가장 긴 문자열 찾기
string longestName = fruits.Aggregate((longest, next) =>
next.Length > longest.Length ? next : longest);
Console.WriteLine("The fruit with the longest name is {0}.", longestName);
Console.Read();
}
}
}
결과

참고
https://learn.microsoft.com/ko-kr/dotnet/api/system.linq.enumerable.aggregate?view=net-7.0
Enumerable.Aggregate 메서드 (System.Linq)
시퀀스에 누적기 함수를 적용합니다. 지정된 시드 값은 초기 누적기 값으로 사용되고 지정된 함수는 결과 값을 선택하는 데 사용됩니다.
learn.microsoft.com
728x90