If I had a method with no operation but to return the passed in parameter would the compiler remove it from the generated MSIL?
For example:
public partial class StringResource {
/// <summary>About</summary>
public static string About { get { return Encode(Resources.WebUI.About); } }
public static string Encode(string s) { return s; }
}
Would the C# compiler even generate the Encode method? How can I look at the generated IL and compare the code with and without the Encode method?
And if the static compiler does not remove it what about the runtime JIT compiler?
No. It couldn’t be – that would break anything which tried to call that method via reflection, etc.
The method could potentially be inlined to nothing by the JIT, but that’s a different matter. There are various things which determined whether methods can be inlined, and those details differ between different versions of the CLR, different process architectures etc. For empty methods, I’d expect them to be inlined (when not running in a debugger) for non-virtual calls.
ildasm Foo.dll /out=after.ilbefore.ilandafter.il