-
[C#] Linq Enumerable.ZipC# Visual Studio/2 2024. 1. 23. 10:06
참조 : https://learn.microsoft.com/ko-kr/dotnet/api/system.linq.enumerable.zip?view=net-5.0
Zip는 데이터 컬렉션(List, Array, Dictionay)의 각각의 요소를 병합할 때 사용됩니다.
메서드는 첫 번째 시퀀스의 각 요소를 두 번째 시퀀스에서 동일한 인덱스가 있는 요소와 병합합니다.
시퀀스에 동일한 수의 요소가 없는 경우 메서드는 시퀀스가 해동 요소 중 하나의 끝에 도달할 때까지 시퀀스를 병합합니다.
예를 들어 한 시퀀스에 3개의 요소가 있고 다른 시퀀스에 4개의 요소가 있는 경우 결과 시퀀스에는 3개의 요소만 있습니다.
네임스페이스 : System.Linq
public static System.Collections.Generic.IEnumerable<TResult> Zip<TFirst,TSecond,TResult> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second, Func<TFirst,TSecond,TResult> resultSelector);
형식 매개 변수
TFirst : 첫 번째 입력 시퀀스 요소의 형식입니다.
TSecond : 두 번째 입력 시퀀스 요소의 형식입니다.
TResult : 결과 시퀀스 요소의 형식입니다.
매개 변수
IEnumerable<TFirst> first : 병합할 첫 번째 시퀀스입니다.
IEnumerable<TSecond> second : 병합할 두 번쨰 시퀀스입니다.
Func<TFirst, TSecond, TResult> resultSelector : 두 시퀀스의 요소를 병합하는 방법을 지정하는 함수입니다.
반환
IEnumerable<TResult> : 두 입력 시퀀스의 병합된 요소가 들어 있는 IEnumerable<T> 입니다.
Zip 메서드 예제 코드
namespace practice { internal class Program { static void Main(string[] args) { string[] words = { "one", "two", "three", "four" }; int[] numbers = { 1, 2, 3, 4, 5, 6, 7 }; var numbersAndWords = words.Zip(numbers, (first, second) => first + " = " + second); foreach (var item in numbersAndWords) Console.WriteLine(item); } } }
'C# Visual Studio > 2' 카테고리의 다른 글
[C#] string.PadLeft, string.PadRight (0) 2024.01.17 [C#] Console.CursorLeft , Console.CursorTop , Console.SetCursorPosition() (0) 2024.01.10 [C#] JsonConvert, StreamWrite, StreamReader, File.Exists (1) 2024.01.04 [C#] string.Compare(String, String), Array.Sort() (0) 2023.12.29