
If you browse through certain libraries or the .NET Runtime from time to time, you will notice that the attribute MethodImplOptions.AggressiveInlining can be found in some places - but what is this actually?
Method Implementations
First of all, it must be said that the C# compiler and the .NET runtime are very clever - they usually know exactly how code should be compiled and executed. But sometimes it can happen that the compiler or the runtime are not as smart as we would like them to be.
By default, a method call leads to its jump in simplified terms: it jumps from method A to method B. The following example code illustrates this:
1public class TestClass
2{
3 public void Check()
4 {
5 string myVar = "true";
6
7 if( IsTrue( myVar ) )
8 {
9 Console.WriteLine( "It's true!" );
10 }
11 else
12 {
13 Console.WriteLine( "It's false!" );
14 }
15 }
16
17 public static bool IsTrue(string value)
18 {
19 if (bool.TryParse(value, out bool boolValue))
20 {
21 return boolValue;
22 }
23
24 return false;
25 }
26}
In this example, the method IsTrue is called. The compiler therefore jumps from Check to IsTrue and back again. And exactly this is an overhead - and the simpler the method, the greater the share of the overhead in the runtime.
Aggressive inlining
To counteract this overhead, we can tell the compiler not to generate the method call, but to insert the content of the method directly into the calling method, if this works. This is known as inlining. The compiler copies code to the appropriate places.
1public class TestClass
2{
3 public void Check()
4 {
5 string myVar = "true";
6
7 if( bool.TryParse(value, out bool boolValue) && boolValue )
8 {
9 Console.WriteLine( "It's true!" );
10 }
11 else
12 {
13 Console.WriteLine( "It's false!" );
14 }
15 }
16}
Benchmark
To be able to demonstrate this at runtime, I have written a small sample .
1public static class StringExtensions
2{
3 public static bool HasUnicode_NonAggressive(string source)
4 {
5 foreach (char c in source)
6 {
7 if (c > 255)
8 {
9 return true;
10 }
11 }
12
13 return false;
14 }
15
16 [MethodImpl(MethodImplOptions.AggressiveInlining)]
17 public static bool HasUnicode_Aggressive(string source)
18 {
19 foreach (char c in source)
20 {
21 if (c > 255)
22 {
23 return true;
24 }
25 }
26
27 return false;
28 }
29}
Both methods do exactly the same thing; it checks character by character whether it is a Unicode character - very simple and efficient. The only difference is the declaration of the attribute [MethodImpl(MethodImplOptions.AggressiveInlining)].
The benchmark looks like this:
1[MemoryDiagnoser]
2[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
3[HideColumns(Column.Job)]
4public class Benchmark
5{
6 [Benchmark]
7 public bool HasUnicode_NonAggressive()
8 {
9 return StringExtensions
10 .HasUnicode_NonAggressive("Hello ๐! This is a test: ๐๐๐
๐ฝ๐ถ, ฮณ, ฮด, ฮต, ฮฉ, ฯ, โ, โค๏ธ");
11 }
12
13 [Benchmark]
14 public bool HasUnicode_Aggressive()
15 {
16 return StringExtensions
17 .HasUnicode_Aggressive("Hello ๐! This is a test: ๐๐๐
๐ฝ๐ถ, ฮณ, ฮด, ฮต, ฮฉ, ฯ, โ, โค๏ธ");
18 }
19}
The result is as follows:
1// * Summary *
2
3BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5247/22H2/2022Update)
4AMD Ryzen 9 9950X, 1 CPU, 32 logical and 16 physical cores
5.NET SDK 9.0.200-preview.0.24575.35
6 [Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
7 .NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
8
9Job=.NET 9.0 Runtime=.NET 9.0
10
11| Method | Mean | Error | StdDev | Ratio | RatioSD |
12|------------------------- |----------:|----------:|----------:|------:|--------:|
13| HasUnicode_NonAggressive | 0.1933 ns | 0.0069 ns | 0.0065 ns | 1.00 | 0.05 |
14| | | | | | |
15| HasUnicode_Aggressive | 0.0073 ns | 0.0040 ns | 0.0037 ns | 1.65 | 2.19 |
See full code here ๐ณ Sustainable Code - Aggressive Inlining ๐
The example shows very impressively that the inlining variant is more than 25 times faster. This shows that the actual work of the method, i.e. checking the characters, is very fast, but the overhead of the method call still plays a huge role. A perfect example of how inlining can have a positive effect on performance - and why it is so powerful.
When should you use it?
- For “small” methods that are called frequently; especially something like extension classes
- When the function call itself takes more time than the actual work of the method.
However, it should be noted that every inlining leads to the code being duplicated and the application becoming larger overall.
This and many other examples of efficient everyday code https://github.com/BenjaminAbt/SustainableCode
Related articles

Jan 15, 2025 - 5 min read
Replace FluentAssertions with AI
New year - new .NET drama. You would have thought that positive lessons had been learned from the last Moq drama; but we were proven wrong.

Jan 13, 2025 - 5 min read
Advanced Usage of MSSQL Indexed Views in EF Core
In the world of Microsoft SQL Server , views are a powerful tool for simplifying complex data queries. Simple views offer no real โฆ

Jan 03, 2025 - 2 min read
Increase Command Timeout for EF Core Migration Bundles
EF Core Migration Bundles are standalone executable files that contain one or more Entity Framework Core migrations and can be applied โฆ
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
