在C#中的内联函数? [英] Inline functions in C#?

查看:1133
本文介绍了在C#中的内联函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何在C#中做内联函数?我不认为我理解这个概念。他们好像匿名方法?像lambda函数?

How do you do "inline functions" in C#? I don't think I understand the concept. Are they like anonymous methods? Like lambda functions?

注意:这些问题的答案几乎完全处理来内联函数的,也就是说手动或编译器优化替换一个函数调用站点被叫者的身体。如果您有兴趣匿名(又名拉姆达)功能的,看的 @ jalf的答案或<一个href=\"http://stackoverflow.com/questions/1085875/what-is-this-lambda-everyone-keeps-speaking-of/1086347#1086347\">What这是拉姆达每个人都保持发言?。

Note: The answers almost entirely deal with the ability to inline functions, i.e. "a manual or compiler optimization that replaces a function call site with the body of the callee." If you are interested in anonymous (a.k.a. lambda) functions, see @jalf's answer or What is this 'Lambda' everyone keeps speaking of?.

推荐答案

最后,在.NET 4.5中,CLR允许一个提示/提示 1 方法使用内联<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions%28v=VS.110%29.aspx\"相对=nofollow> MethodImplOptions.AggressiveInlining 值。这也是在Mono的躯干(今承诺)提供。

Finally in .NET 4.5, the CLR allows one to hint/suggest1 method inlining using MethodImplOptions.AggressiveInlining value. It is also available in the Mono's trunk (committed today).

// The full attribute usage is in mscorlib.dll,
// so should not need to include extra references
using System.Runtime.CompilerServices; 

...

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void MyMethod(...)

1 即可。 previously力用在这里。由于有几个downvotes,我会尽力澄清术语。作为评价和文档,的方法应该是,如果可能的内联。特别是考虑到单声道(这是打开的),有一些具体的单技术限制考虑内联或更多一般的(如虚拟函数)。总体来说,是的,这是一个提示编译器,但我想这是什么要求。

1. Previously "force" was used here. Since there were a few downvotes, I'll try to clarify the term. As in the comments and the documentation, The method should be inlined if possible. Especially considering Mono (which is open), there are some mono-specific technical limitations considering inlining or more general one (like virtual functions). Overall, yes, this is a hint to compiler, but I guess that is what was asked for.

这篇关于在C#中的内联函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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