C#:FUNC<>而不是方法? [英] C#: Func<> instead of methods?

查看:151
本文介绍了C#:FUNC<>而不是方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在知道一个好奇的问题为大家:

有没有伤害/缺点使用函数功能,而不是一个方法?简单的例子:

 私有静态函数功能:LT; INT,INT,的DbContext,名单,其中,T>>富=
    (I1,I2,DBC)=>
        (I1!= 0)? dbc.Bar(I2):新的List< T> {/ *某些默认值... * /};
 

VS

 私有静态列表< T>美孚(INT I1,I2 INT,的DbContext DBC)
{
    返回的i1!= 0? dbc.Bar(I2):新的List< T> {/ *某些默认值... * /};
}
 

解决方案

我看到severale缺点:

  • 在性能的影响(委托VS法) - 小,但它的存在
  • 在没有parameternames(伤害可读性上的调用)
  • 定义本身是不易阅读
  • 无过载可能的(感谢萨那托斯)

,你获得什么,我只会这么一个地方,小环境和preFER静态方法

This is a curiosity questions for you all in the know:

Is there any harm/downside to using a Func instead of a method? Simple example:

private static Func<int, int, DBContext, List<T>> Foo =
    (i1, i2, dbc) =>
        (i1 != 0) ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ };

Vs

private static List<T> Foo(int i1, int i2, DBContext dbc)
{
    return i1 != 0 ? dbc.Bar(i2) : new List<T> { /*some default values ...*/ };
}

解决方案

I see severale downsides:

  • performance impact (delegate vs method) - small but it's there
  • no parameternames (hurts readability on calls)
  • the definition itself is less readable
  • no overload possible (thanks to xanatos)

as you gain nothing I would only to so in a local and small context and prefer the static method

这篇关于C#:FUNC&LT;&GT;而不是方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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