C#Aggregateアキュムレータ

C#Aggregateアキュムレータ

アキュムレーターとは?アキュムレータの使い方は?心配しないでください、新技術の誕生は、基本的に特定のニーズを満たすことであり、ニーズから、この機能の特性を理解しやすくなります。

最后更新 2022/04/20 7:11
遇水寒
预计阅读 4 分钟
分类
.NET
标签
.NET C# Aggregate

1. 需要は

アキュムレーターとは?アキュムレータの使い方は?心配しないでください、新技術の誕生は、基本的に特定のニーズを満たすことであり、ニーズから、この機能の特性を理解しやすくなります。

理解を容易にするために、5つの数字をするint 1次元配列があるとし、最初の値-1に2つ目の値0の和-1を求め、その値-1を配列の3つ目の数3に加算し、再び2を配列の4つ目の数5に加算する。このようにして、int result = -1 + 0 + 3 + 5 + 8;

int[] numbers={-1, 0, 3, 5,8};

1.1基本的なニーズ

//V1.0版本
static void Main(string[] args)
{
    int[] numbers = { -1, 0, 3, 5, 8 };
    int result = numbers[0];
    for (int i = 1; i < numbers.Length; i++)
    {
        result = result + numbers[i];
    }
    Console.WriteLine(result);//输出15
    Console.ReadKey();
}

1.2カプセル化アルゴリズムの一部

中間のアルゴリズムを取り出して、異なる長さのint型の配列を入力できるようにします:

//V1.1 版本
static void Main(string[] args)
{
    int[] numbers = { -1, 0, 3, 5, 8 };
    var result = Aggregate(numbers);
    Console.WriteLine(result);
    Console.ReadKey();
}

static int Aggregate(int[] array)
{
    int result = array[0];
    for (int i = 1; i < array.Length; i++)
    {
        result = result + array[i];
    }
    return result;
}

1.3加算だけでなくアルゴリズムの置換

アキュムレータは加算するだけではありません。

int result = -1 * 0 * 3 * 5 * 8;

int result = -1 - 0 - 3 - 5 - 8;
//V1.2 版本,实现算法替换
//需了解委托,Lambda相关知识
static void Main(string[] args)
{
    int[] numbers = { -1, 0, 3, 5, 8 };
    var result = Aggregate(numbers,(result,next)=>result * next);//实现乘法
    //var result = Aggregate(numbers,(result,next)=>result - next);//实现减法
    Console.WriteLine(result);
    Console.ReadKey();
}

static int Aggregate(int[] array,Func<int,int,int> func)
{
    int result = array[0];
    for (int i = 1; i < array.Length; i++)
    {
        result = func(result, array[i]);
    }
    return result;
}

1.4 int型だけでなくジェネリック

目前该算法int Aggregate(int[] array,Func<int,int,int> func)只支持 int 类型,现在要扩展到任意类型–泛型。

//V1.3 版本,实现泛型
//需了解泛型,IEnumerator接口相关知识
static void Main(string[] args)
{
    int[] numbers = { -1, 0, 3, 5, 8 };
    var result = Aggregate(numbers, (result, next) => result + next);
    Console.WriteLine(result);
    Console.ReadKey();
}

static TSource Aggregate<TSource>(IEnumerable<TSource> source,Func<TSource, TSource, TSource> func)
{
    using (IEnumerator<TSource> e = source.GetEnumerator())
    {
        if (!e.MoveNext())
        {
            throw new ArgumentException();
        }

        TSource result = e.Current;
        while (e.MoveNext())
        {
            result = func(result, e.Current);
        }

        return result;
    }
}

1.5実装方法が拡張方法になる

IEnumeratorインタフェースを実装したクラスに直接拡張メソッドを追加します。この時点で、Aggregateは公式サイトが提供するアキュムレータと同様の機能を実装しています。

//V1.4 版本,扩展方法
class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { -1, 0, 3, 5, 8 };
        var result = numbers.Aggregate((result, next) => result + next);
        Console.WriteLine(result);
        Console.ReadKey();
    }
}

public static class Helper
{
    public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
    {
        using (IEnumerator<TSource> e = source.GetEnumerator())
        {
            if (!e.MoveNext())
            {
                throw new ArgumentException("需要至少两个元素");
            }

            TSource result = e.Current;
            while (e.MoveNext())
            {
                result = func(result, e.Current);
            }

            return result;
        }
    }
}

2. マイクロソフトが提供するAPI

System.Linq名前空間の下には、次のものがあります。

1. public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);

2. public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);

3. public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);

最初のインターフェイスは、上記の例と同じ機能を備えています。他の2つの機能は後で説明します(時間があれば)。

3.浅いから深い。

如果是初学者,可能大脑第一反应还是觉得这个Aggregate只能加减乘除,如果这样想,说明其实还是理解得不够深刻,现在来深度剖析该方法的传参:func

下の図に示すように、アキュムレータは各要素をトラバースします。最初は、最初と2番目の要素を直接func演算に取ります。3番目からは、最後の計算結果resultをfuncの最初の入力引数として取り、2番目の入力引数は配列の次の要素であり、最後にトラバースし、最終的な結果値を返します。

実際の使用:データベース内のテーブルが複数のテーブルの外部キーを参照している場合、これらの外部キーをクエリする必要がある場合、アキュムレータを使用してクエリする必要がある条件を累積し、データベースにクエリすることができます。

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 2026/04/22

バージョン別の. NETサポート状況(250 7 0 7更新)

仮想マシンとテストマシンを使用して、各バージョンのオペレーティングシステムの. NETサポートをテストします。オペレーティングシステムのインストール後、対応するランタイムを測定し、スターダストエージェントをパスとして実行できます。

继续阅读
同分类 / 同标签 2026/02/07

AOTの使用経験

プロジェクトの最初から、新しい機能が追加されたり、新しい構文が使用されたりするたびに、AOTリリーステストを行うという良い習慣を身につける必要があります。

继续阅读