Be careful with EIMI.

  1. Why do we have to be careful with EIMI?
  2. How to manage with the problem of deriving when we are using EIMI.

We have three problems with using EIMI:

1. The first one is that is ambiguous that we can call a method of an interface, because IntelliSense doesn’t support EIMI.

2. The second is that when we cast to an interface we have boxing.

3. The third is that we can’t call from a derived class an explicitly implemented method of its base class.

 

Example 1:

        static void Main(string[] args)

        {

            int x = 0;

            //We have two problems here.

            //The first one is that is ambiguous that we can call a method of IConvertible,

            //becasue InteliSense doesn’t work.

            //The second is that when we cast to the IConvertible interface we have boxing

            double y = ((IConvertible)x).ToDouble(null);

        }

 

The example above shows us the first two problems. The first one is that is ambiguous that we can call a method of the IConvertible, because IntelliSense doesn’t support EIMI. The second is that when we cast to the IConvertible interface we have boxing.

 

 

Example 2:

 

    public class Base : IComparable

    {

        int m_x = 0;

 

        int IComparable.CompareTo(Object obj)

        {

            return m_x – ((Base)obj).m_x;

        }

 

    }

 

    public class Derived : Base, IComparable

    {

        public int CompareTo(Object obj)

        {

            //Compile error: Base does not contain a definition for ‘CompareTo’

            //We can’t call base CompareTo method because it is private           

            return base.CompareTo(obj);

        }

    }

 

The example 2 shows us the third problem. We can’t call base CompareTo method because it is private.

Example 3:

 

    public class Base : IComparable

    {

        int m_x = 0;

 

        int IComparable.CompareTo(Object obj)

        {

            return m_x – ((Base)obj).m_x;

        }

 

        //We cannot mark this method as protected or private,

        //because a derived class can’t contain a method with a more accessible level.

        public virtual int CompareTo(Object obj)

        {

            return m_x – ((Base)obj).m_x;

        }

    }

 

    public class Derived : Base, IComparable

    {

        //To solve the problem of deriving when we are using EIMI

        //we override the method of the base class

        public override int CompareTo(Object obj)

        {

            return base.CompareTo(obj);

        }

    }

The example 3 shows us the solution of the third problem. We can implement an absolutely new method with the interface signature. And the method can be called from the derived class. To avoid warning about that the Derived.CompareTo() method hides the Base.CompareTo() we can mark the Base.CompareTo() as virtual and override it in the derived class. Unfortunately, we can’t set an accessible level of the base method higher than public because we must have access to the derived method and derived methods can’t have an access level lower than its base methods.

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