将函数从C#转换为Lambda [英] Convert a function from c# to Lambda

查看:51
本文介绍了将函数从C#转换为Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我写了这个函数,如下所示:

Hello All,

I have written this function which is as follows:

public static long ByteArrayToLong(byte[] input)
        {
            long accumulator=0;
            byte counter = 0;


            for (counter=0; counter<input.Length -1; counter++)
                accumulator += (input[counter] & 0xff) << (8 * counter);

            return accumulator;

        }


有人可以告诉我如何在lambda中做到这一点吗?


Could anybody tell me how I can do this in lambda please?

Many thanks in advance.

推荐答案

Func<byte[], long> f = (input) => 
{
  long accumulator=0;
  byte counter = 0;
 

  for (counter=0; counter<input.Length -1; counter++)
    accumulator += (input[counter] & 0xff) << (8 * counter);
 
  return accumulator;
}
long result = f(new byte[] {0,4,1});



或:



Or:

data.Select((value, index) => new KeyValuePair<int, byte>(index, value)).Sum(k => (k.Value & 0xff) << (8 * k.Key));


这篇关于将函数从C#转换为Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆