Implicit and Explicit interface method implementations

  1. What is happening with the methods table when we create a type? Which entries contain method table.
  2. The difference between implicit and explicit  interface method implementation
  3. What is the EIMI (explicit interface method implementation)?

 

Example 1:

    internal class DisposableSample : IDisposable

    {

        //Actually, more then one entry were created in the method table.

        //One for the DisposableSample the second for the IDisposable interface and

        //several entries were created for instance method defined by System.Object

        public void Dispose()

        {

            Console.WriteLine("DisposableSample.Dispose");

        }

    }

1. The method table of this type contains:

1. The base classes’ entries like the System.Object.

2. The Interface methods entries (the Dispose() method which is belonging to the IDisposable).

3. The new methods entries (the Dispose() method which is belonging to the DisposableSample)

So, actually the method table contains entries for its base types as well as for its new methods.

Example 2:

    class Program

    {

        static void Main(string[] args)

        {

            DisposableSample ds = new DisposableSample();

            ds.Dispose();

 

            IDisposable ids = ds;

            ids.Dispose();

 

            Console.ReadKey();

        }

    }

2. This example shows that the C# compiler assumes that the Dispose() method is implementation of the method of the IDisposable interface. The C# compiler does this things because the method has public accessibility and its signature the same as the Dispose() method of the IDisposable interface.

In this case the C# compiler emits metadata which make methods entries refer to the same implementation.  

Example 3:

    internal class DisposableSample : IDisposable

    {

        void IDisposable.Dispose()

        {

            Console.WriteLine("IDisposable.Dispose");

        }

 

        public void Dispose()

        {

            Console.WriteLine("DisposableSample.Dispose");

        }

    }

3. To make my own interface method implementation, I must write an interface name as the prefix before a method name and I’m not allowed to specify any accessibility. However, when the C# compiler generates metadata for this method it sets method to private. Programmers can call this method only through a variable of the interface’s type. Such kind of an implementation calls EIMI (explicit interface method implementation).

When I’m using EIMI I’m not allowed to mark method as virtual, so method cannot be overridden. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s