IL Interface Code

I am using LinqPad and I was looking at the IL code (compiler optimization enabled) generated for the following interface and a 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 do not see any IL code for the interface. Is this because of how LinqPad works, or does the C # compiler treat interfaces differently?

+6
source share
1 answer

IL - for method bodies. The interface does not contain any method bodies, therefore, not IL.

The body of the method in this case (simply) is any executable code. Nothing in the interface is executable, since it is just a contract. It is the interface developer who is provided with an implementation that usually contains IL.

+16
source

Source: https://habr.com/ru/post/906781/


All Articles