Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

The power of Aggressive Inlining in .NET and C#

The power of Aggressive Inlining in .NET and C#
January 20, 2025 4 min read BEN ABT
Csharp .NET

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:

C#
 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.

C#
 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 .

C#
 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:

C#
 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:

Shell
 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

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not โ€ฆ

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built โ€ฆ


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

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.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer ยท Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in โ€ฆ

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

ยฉ Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact