Exploring the .NET CoreFX Part 12: Aggressive Inlining
Exploring the .NET CoreFX .net core csharp system.collections.immutable
Published: 2015-01-05
Exploring the .NET CoreFX Part 12: Aggressive Inlining

This is part 12/17 of my Exploring the .NET CoreFX series.

In C++, the inline keyword allows a developer to provide a hint to the compiler that a particular method should be inlined. C# has the identical ability but uses an attribute instead:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
internal class SecurePooledObject<T>
{
    ....

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    internal bool IsOwned<TCaller>(ref TCaller caller)
        where TCaller : struct, ISecurePooledObjectUser
    {
        return caller.PoolUserId == _owner;
    }
}

In System.Collections.Immutable, this attribute is used highly selectively – only once, in fact.

Recommendations

  • In rare cases, consider using MethodImpl(MethodImplOptions.AggressiveInlining) to suggest to the .NET runtime that a particular method should be inlined.