C#:FUNC<>而不是方法?而不是、方法、FUNC、LT

2023-09-04 01:12:27 作者:洎莋哆情

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

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

 私有静态函数功能: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> {/ *某些默认值... * /};
}
 
空中比特币,ltcgs,假外汇之后,又一种类似这种模式的传销骗局来袭

解决方案

我看到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