C# 모듈

1250 단어 csharp
C#7.0에서 시작하여 원조can be expressed like this:
(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.

(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.
이렇게 하면 너는 쓸 필요가 없다new Tuple(item1, item2).
원조는 방법 서명에서 간결하게 표시할 수 있다.예를 들어 이 방법은 일반 유형IEnumerable의 원조T를 되돌려줍니다.
public static IEnumerable<(T,T)> Pairs<T>(IEnumerable<T> ts)
{
    var skip = 1;
    T previous = default;
    foreach (var current in ts)
    {
        if (skip == 0)
        {
            yield return (previous, current);
        }
        else
        {
            skip--;
        }
        previous = current;
    }
}
이 방법은 쌍을 이루는 anIEnumerable을 통해 매거한다.
예를 들어 new [] { 1,2,3 }가 있으면 다음과 같은 원조(신문법 사용)를 열거한다. (1,2),(2,3).

좋은 웹페이지 즐겨찾기