Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force inline functions in C#? [duplicate]

Tags:

c#

.net

Possible Duplicate:
Inline functions in C#?

In c++ we can force function inlining.

Is this also possible in c#? sometimes, and when the method is small, it gets inlined automatically. But is it possible force inlining functions in c#/.Net?

like image 562
Chris Avatar asked Sep 06 '12 15:09

Chris


1 Answers

Sort of. It's not under your direct control to turn on for sure. It's never inlined in the IL - it's only done by the JIT.

You can explicitly force a method to not be inlined using MethodImplAttribute

[MethodImpl(MethodImplOptions.NoInlining)] public void Foo() { ... } 

You can also sort of "request" inlining as of .NET 4.5:

[MethodImpl(MethodImplOptions.AggressiveInlining)] public void Foo() { ... } 

... but you can't force it. (Prior to .NET 4.5, that enum value didn't exist. See the .NET 4 documentation, for example.)

like image 68
Jon Skeet Avatar answered Sep 28 '22 08:09

Jon Skeet