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?
Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. Some of the interface types in C# include. IEnumerable − Base interface for all generic collections. IList − A generic interface implemented by the arrays and the list type.
In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X". Again, as an example, anything that "ACTS LIKE" a light, should have a turn_on() method and a turn_off() method.
An interface is a specification for a set of class members, not an implementation. An Interface is a reference type and it contains only abstract members such as Events, Methods, Properties etc. It contain only declaration for its members and implementation defined as separate entities from classes.
An Interface in C# is used along with a class to define a contract which is an agreement on what the class will provide to an application. The interface defines what operations a class can perform. An interface declares the properties and methods. It is up to the class to define exactly what the method will do.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With