[英]Function chaining in c# using extension methods on delegates. Is there any performance implications. I am posting code snippet with usage
Extension on Delegate.代表扩展。 Suggestion and any improvements are welcomed.
欢迎提出建议和任何改进。 This code is being currently used in the one of the projects.
该代码目前正在其中一个项目中使用。
internal static class xt
{
public static Func<From, To> Next<From, To>(this From @obj, Func<From, To> transform)
{
return (fr) => transform(fr);
}
public static Func<Tform, TForward> Next<Tform, Tto, TForward>(this Func<Tform, Tto> g, Func<Tto, TForward> f)
{
return (tf) => f(g(tf));
}
public static Tto Input<Tfrom, Tto>(this Func<Tfrom, Tto> fx, Tfrom data)
{
return fx(data);
}
public static void Execute(this Action fx)
{
fx();
}
public static Func<Tform, Tto> Next<Tform, Tto>(this Func<Tform> g, Func<Tform, Tto> f)
{
return tf => f(g());
}
}
The only performance implications you face is the memory allocation for delegates.您面临的唯一性能影响是代表的 memory 分配。 However, these are small and short-lived, will live in Gen0 and be collected in a quick operation.
然而,这些都是小而短暂的,将存在于 Gen0 并在快速操作中收集。
I don't think you should be concerned about it.我认为你不应该担心它。 There are usually way more performance-threatening elements in the code you should be concerned about - string processing, array processing, etc.
您应该关注的代码中通常有更多威胁性能的元素 - 字符串处理、数组处理等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.