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

查看:29
本文介绍了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?

注意:答案几乎完全涉及内联函数,即手动或编译器优化,用被调用者的主体替换函数调用站点".如果您对匿名(又名 lambda)函数感兴趣,请参阅@jalf 的回答每个人都在谈论的Lambda"是什么?.

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 允许使用 MethodImplOptions.AggressiveInlining

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.以前的强制"在这里使用.我会尽力澄清这个词.正如在评论和文档中一样,如果可能的话,方法应该被内联. 特别是考虑到 Mono(它是开放的),考虑到内联或更通用的技术限制(例如虚函数),存在一些特定于单声道的技术限制).总的来说,是的,这是对编译器的一个提示,但我想这就是所要求的.

1. Previously "force" was used here. 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天全站免登陆