Sherry Sherry - 3 years ago 219
C# Question

What is the difference between Expression bodied syntax vs Getter syntax on IL level?

I would like to understand the IL code a bit.

So as you see the expression bodied has less code than the GetOld code. Is it some optimization done there and means expression body syntax is more performant?

Or it does not really matter?

namespace DatabaseModules {
public class Test {
public IList<string> _cache = new List<string>();

public Test() {

}

public IList<string> Get => _cache;

public IList<string> GetOld {
get { return _cache; }
}
}
}


And the generated IL Code using DotPeek

https://gist.github.com/anonymous/9673389a1a21d0ad8122ec97178cfd9a

Answer Source

There is none. That code compiles to the exact same C# using Roslyn, so the IL is no different:

using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;

[assembly: AssemblyVersion("0.0.0.0")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[module: UnverifiableCode]
namespace DatabaseModules
{
    public class Test
    {
        public IList<string> _cache = new List<string>();

        public IList<string> Get
        {
            get
            {
                return this._cache;
            }
        }

        public IList<string> GetOld
        {
            get
            {
                return this._cache;
            }
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download