Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IL Code of an interface

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?

like image 550
Pawan Mishra Avatar asked Jan 24 '12 18:01

Pawan Mishra


People also ask

What is interface type in C#?

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.

What is an interface in oops?

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.

WHAT IS interface in asp net?

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.

WHAT IS interface in C# with real time example?

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.


1 Answers

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.

like image 58
vcsjones Avatar answered Oct 15 '22 01:10

vcsjones