I am using LinqPad and in that I was looking at the IL code(compiler optimization switched on) generated for the following Interface & the class that implements the interface:
public interface IStockService
{
[OperationContract]
double GetPrice(string ticker);
}
public class StockService : IStockService
{
public double GetPrice(string ticker)
{
return 93.45;
}
}
IL Code :
IStockService.GetPrice:
StockService.GetPrice:
IL_0000: ldc.r8 CD CC CC CC CC 5C 57 40
IL_0009: ret
StockService..ctor:
IL_0000: ldarg.0
IL_0001: call System.Object..ctor
IL_0006: ret
Surprisingly I don’t see any IL code for the interface. Is this because of the way LinqPad works or the C# compiler treats the interfaces in a different manner?
IL is for method bodies. An interface does not contain any method bodies, thus no IL.
A method body in this case (put simply) is any executable code. Nothing in an interface is executable since it is just a contract. It’s up to the implementor of the interface to provide an implementation, which will usually contain IL.