I found this interesting article on C# inline methods.
Scope of a method doesn't appear to change when a function becomes an inline candidate. Here is a summary of that article.
How to determine what get’s inlined? The short answer is that you can’t. The MSDN article “Writing High-Performance Managed Applications : A Primer” gives the following guidance:
- Methods that are greater than 32 bytes of IL will not be inlined.
- Virtual functions are not inlined.
- Methods that have complex flow control will not be in-lined. Complex flow control is any flow control other than if/then/else; in this case, switch or while.
- Methods that contain exception-handling blocks are not inlined, though methods that throw exceptions are still candidates for inlining.
- If any of the method’s formal arguments are structs, the method will not be inlined.
There is no
inline keyword in C#, but there is an inline compiler directive that was introduced in .NET 4.5 called MethodImplOptions.AggressiveInlining
It can be used like this.
static int Method2()
// ... Aggressive inlining.
return "one".Length + "two".Length + "three".Length +
"four".Length + "five".Length + "six".Length +
"seven".Length + "eight".Length + "nine".Length +
You can't know for sure if the compiler will inline a function. This isn't feature C# developers have control over.