您以何种方式使用C#Lambda表达式?

时间:2021-03-18 22:32:45

Pour in your posts. I'll start with a couple, let us see how much we can collect.

倒入你的帖子。我将从一对夫妇开始,让我们看看我们可以收集多少钱。

To provide inline event handlers like

提供类似的内联事件处理程序

button.Click += (sender,args) =>
{
};

To find items in a collection

查找集合中的项目

 var dogs= animals.Where(animal => animal.Type == "dog");

For iterating a collection, like

用于迭代集合,例如

 animals.ForEach(animal=>Console.WriteLine(animal.Name));

Let them come!!

让他们来!!

7 个解决方案

#1


3  

Returning an custom object:

返回自定义对象:

var dude = mySource.Select(x => new {Name = x.name, Surname = x.surname});

#2


2  

One line function

一线功能

Func<int, int> multiply = x => x * 2;
int y = multiply(4);

#3


2  

Here's a slightly different one - you can use them (like this) to simulate the missing "infoof"/"nameof" operators in C# - i.e. so that instead of hard-coding to a property name as a string, you can use a lambda. This means that it is validated at compile time (which strings can't be).

这是一个稍微不同的 - 你可以使用它们(像这样)来模拟C#中缺少的“infoof”/“nameof”运算符 - 也就是说,不是硬编码为属性名称作为字符串,你可以使用lambda 。这意味着它在编译时验证(不能使用哪些字符串)。

There is obviously a performance cost to this, hence "just for fun", but interesting...

这显然是一个性能成本,因此“只是为了好玩”,但有趣......

#4


1  

With method invoker to update UI from a multi threaded componenet event

使用方法调用程序从多线程组件事件更新UI

void Task_Progress(object sender,TaskProgressArgs  e)
{
    BeginInvoke(new MethodInvoker(() => UpdateProgress(e)));
}

#5


0  

Creating an accumulator.

创建累加器。

    static Func<int, int> Foo(int n)
    {
        return a => n += a;
    }

Note the closure usage here. it's creating an accumulator that "remembers" the value of n between calls - without a class or instance variable.

请注意这里的闭包用法。它正在创建一个累加器,它“记住”调用之间的n值 - 没有类或实例变量。

#6


0  

For aggregate operations with Linq:

对于与Linq的集合操作:

public Double GetLengthOfElements(string[] wordArr) {

   double count = wordArr.Sum(word => word.Length);
   return count;
}

Sure beats using foreach

肯定是使用foreach节拍

#7


-1  

To express an unnamed function.

表达一个未命名的功能。

#1


3  

Returning an custom object:

返回自定义对象:

var dude = mySource.Select(x => new {Name = x.name, Surname = x.surname});

#2


2  

One line function

一线功能

Func<int, int> multiply = x => x * 2;
int y = multiply(4);

#3


2  

Here's a slightly different one - you can use them (like this) to simulate the missing "infoof"/"nameof" operators in C# - i.e. so that instead of hard-coding to a property name as a string, you can use a lambda. This means that it is validated at compile time (which strings can't be).

这是一个稍微不同的 - 你可以使用它们(像这样)来模拟C#中缺少的“infoof”/“nameof”运算符 - 也就是说,不是硬编码为属性名称作为字符串,你可以使用lambda 。这意味着它在编译时验证(不能使用哪些字符串)。

There is obviously a performance cost to this, hence "just for fun", but interesting...

这显然是一个性能成本,因此“只是为了好玩”,但有趣......

#4


1  

With method invoker to update UI from a multi threaded componenet event

使用方法调用程序从多线程组件事件更新UI

void Task_Progress(object sender,TaskProgressArgs  e)
{
    BeginInvoke(new MethodInvoker(() => UpdateProgress(e)));
}

#5


0  

Creating an accumulator.

创建累加器。

    static Func<int, int> Foo(int n)
    {
        return a => n += a;
    }

Note the closure usage here. it's creating an accumulator that "remembers" the value of n between calls - without a class or instance variable.

请注意这里的闭包用法。它正在创建一个累加器,它“记住”调用之间的n值 - 没有类或实例变量。

#6


0  

For aggregate operations with Linq:

对于与Linq的集合操作:

public Double GetLengthOfElements(string[] wordArr) {

   double count = wordArr.Sum(word => word.Length);
   return count;
}

Sure beats using foreach

肯定是使用foreach节拍

#7


-1  

To express an unnamed function.

表达一个未命名的功能。