动作/ FUNC VS方法,有什么意义呢? [英] Action/Func vs Methods, what's the point?

查看:225
本文介绍了动作/ FUNC VS方法,有什么意义呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用动作函数功能在.NET中,但每一次我开始时,相同的解决方案可以用常规的老方法,我称之为替代来实现。一个使用动作函数功能

I know how to use Action and Func in .NET, but every single time I start to, the exact same solution can be achieved with a regular old Method that I call instead.

这不包括作为东西我不要管,像LINQ的。凡参数。

This excludes when an Action or Func is used as an argument for something I don't control, like LINQ's .Where.

所以基本上我的问题是...为什么这些存在吗?他们是怎么给我加,新,一个简单的方法没有?

So basically my question is...why do these exist? What do they give me extra and new that a simple Method doesn't?

推荐答案

Action和Func键是框架提供的<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/ms173171%28v=vs.80%29.aspx>代表类型。代表们允许函数变量一样对待,这意味着你可以(除其他事项外)的方式传递他们的方法。如果你曾经在C ++编程,你能想到的代表作为函数指针由他们指的是方法的签名受到限制的。

Action and Func are framework-provided Delegate types. Delegates allow functions to be treated like variables, meaning that you can (among other things) pass them from method to method. If you have ever programmed in C++, you can think of Delegates as function pointers that are restricted by the signature of the method they refer to.

Action和Func键特别是通用代表们(这意味着他们需要类型参数)与一些最常见的signatures-在大多数程序中几乎任何方法都可以使用一个或其他这两个来表示的,救人了大量的时间手工定义代表就像我们在之前的.NET做到第2版。事实上,当我在一个项目中看到这样的代码,我通常可以安全地假定该项目从.NET 1.1迁移:

Action and Func specifically are generic delegates (meaning they take type parameters) with some of the most common signatures- almost any method in most programs can be represented using one or the other of those two, saving people a lot of time manually defining delegates like we did in .net prior to version 2. In fact, when I see code like this in a project, I can usually safely assume that the project was migrated from .net 1.1:

// This defines a delegate (a type that represents a function)
// but usages could easily be replaced with System.Action<String>
delegate void SomeApplicationSpecificName(String someArgument);



我建议你看看代表更多一些。他们是C#语言的一个非常强大的功能。

I'd recommend that you look into delegates some more. They are a hugely powerful feature of the C# language.

这篇关于动作/ FUNC VS方法,有什么意义呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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