Bixel
Bixel

Reputation: 75

Understanding this Lambda expression

Found this while learning on a tutorial, having trouble understanding it, as the source code gives no comment on what it is doing. I passed many different byte[] values to it to see if I can figure it out, I can't. And most Lambda tutorials only show a single variable type - not 2.

using System.Linq;
using System.Numerics;

private string DoLamdaExpression(byte[] data)
{
    var intData = data.Aggregate<byte, BigInteger>(0, (current, t) => current * 256 + t);
    string result = intData.ToString();
    return result;
}

current and t are not defined anywhere previously so honestly, I do not know what the role of "byte[] data" is being used other than calling the .Aggregate function.

additionally data.Aggregate = the Accumulate function... my gut feeling is that "byte" is taking the role of current, and "BigInteger" is taking the role of t.

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

Upvotes: 2

Views: 63

Answers (1)

Mureinik
Mureinik

Reputation: 311308

The lambda expression defines an anonymous Func that aggregates the data byte array, where each element of the array is processed. current represents the result so far and t is the element being processed. In this example, the function iterates the array, and for each element, multiplies the current result by 256 and adds the element being handled.

Upvotes: 6

Related Questions