为什么动作/函数比净常规方法更好呢? [英] Why is Action/Func better than a regular Method in .Net?

查看:147
本文介绍了为什么动作/函数比净常规方法更好呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更preFER使用一个动作或一个函数功能,如果我需要一个快速的可重复使用的一块code,但是其他人在我的球队不喜欢他们或了解他们。
此刻我唯一真正的参数是约preference,更是最新的code的做法,但这些都只是坏的争论。

I much prefer using an Action or a Func if I need a quick reusable piece of code, however others on my team don't like them or understand them.
At the moment my only real argument is about preference and more up to date code practices, but those are just bad arguments.

为什么能够更好地做到这一点:

Why is it better to do this:

Action<FormView,String> hideControl = (form,name) => {
    var button = form.GetControl<Button>(name);
    if (button != null)
        button.Visible = false;
}

public static void HideControl<T>(this FormView form, string name) where T : Control
{
    var button = form.GetControl<Button>(name);
    if (button != null)
        button.Visible = false;
}

谁能给我坚实的混凝土参数/例子?

Can anyone give me solid concrete arguments/examples?

推荐答案

有双方你的问题:外观和性能

There are two sides to your question: style and performance.

有关code风格,采用委托做事情有点凌乱。有与code(匿名方法),参数类型推断,以及过度使用时,这可能会导致含有大量的code代表中较小的独立块大功能块体相关的函数名。这不是pretty的看,你可能很难找出了code其实是干什么的,而且很难找到你要找的内容。

For code style, using delegates makes things a little messy. There is no function name associated with the body of code (anonymous method), argument types are inferred, and when overused this can lead to large function blocks containing lots of smaller independent chunks of code in delegates. It's not pretty to look at, it can be hard to figure out what the code is actually doing, and it's hard to find what you're looking for.

有关性能,代表们提出的间接不是一样快,一个普通的老方法来执行。的差小,但用来过量时,它可能成为显

For performance, delegates introduce an indirection that is not as fast to execute as a plain old method. The difference is small, but when used to excess it could become noticeable.

任何工具都有合适的和过度使用。这是适合使用操作或FUNC键回调参数,当你想传递一个回调函数到一个函数。用行动或函数功能作为替代声明函数通常是一个误操作,海事组织。

Any tool has appropriate and excessive uses. It's appropriate to use Action or Func for a callback parameter when you want to pass a callback routine into a function. Using Action or Func as a replacement for declaring functions in general is a misuse, IMO.

这篇关于为什么动作/函数比净常规方法更好呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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