locked
C# Interface Private Implementation RRS feed

  • Question

  • Hi C# experts,

    According to C# The Complete Reference by Herbert Schildt, example code for C# Interface Private Implementation is as follows:

    using System;
    interface IEven
    {
        bool isOdd(int x);
        bool isEven(int x);
    }
    class MyClass : IEven
    {
        bool IEven.isOdd(int x)
        {
            if ((x % 2) != 0) return true;
            else return false;
        }
        public bool isEven(int x)
        {
            IEven o = this;
            return !o.isOdd(x);
        }
    }
    class Demo
    {
        public static void Main()
        {
            MyClass ob = new MyClass();
            bool result;
            result = ob.isEven(4);
            if (result)
            {
                Console.WriteLine("4 is even.");
            }
            IEven iRef = (IEven)ob; //Why on earth , explicit cast? 
            //IEven iRef = ob; // Works fine without explicit cast
            result = iRef.isOdd(3);
            if (result)
            {
                Console.WriteLine("3 is odd.");
            }
            Console.ReadKey();
        }
    }

    Please note in the above code snippet that the object of type MyClass is passed to the reference variable of IEven interface by explicit cast while the code works fine without any explicit cast.

    The question is that do you really need an explicit cast here? What are the pros and cons of not using an explicit cast? Any caveat?

    Please let experts answer.

    Thanks

    Wednesday, December 18, 2013 10:33 AM

Answers

  • "The question is that do you really need an explicit cast here?"

    No. Because MyClass implements IEven there's an implicit conversion from MyClass to IEven so, as you already discovered, the explicit cast isn't required. It's simply an oversight of the author, probably caused by the fact that a cast is required if you want to call isOdd directly on ob:

    ((IEven)ob).isOdd(3); // this requires a cast, isOdd can only be called via the interface and there's nothing that would cause an implicit conversion to be performed in this context

    • Marked as answer by recherche Thursday, December 19, 2013 5:26 AM
    Wednesday, December 18, 2013 3:36 PM

All replies

  • see this link

    http://stackoverflow.com/questions/619716/why-cast-to-an-interface


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    • Proposed as answer by Deepak Kalra Wednesday, December 18, 2013 11:27 AM
    • Unproposed as answer by recherche Thursday, December 19, 2013 5:41 AM
    Wednesday, December 18, 2013 11:27 AM
  • "The question is that do you really need an explicit cast here?"

    No. Because MyClass implements IEven there's an implicit conversion from MyClass to IEven so, as you already discovered, the explicit cast isn't required. It's simply an oversight of the author, probably caused by the fact that a cast is required if you want to call isOdd directly on ob:

    ((IEven)ob).isOdd(3); // this requires a cast, isOdd can only be called via the interface and there's nothing that would cause an implicit conversion to be performed in this context

    • Marked as answer by recherche Thursday, December 19, 2013 5:26 AM
    Wednesday, December 18, 2013 3:36 PM
  • "The question is that do you really need an explicit cast here?"

    No. Because MyClass implements IEven there's an implicit conversion from MyClass to IEven so, as you already discovered, the explicit cast isn't required. It's simply an oversight of the author, probably caused by the fact that a cast is required if you want to call isOdd directly on ob:

    ((IEven)ob).isOdd(3); // this requires a cast, isOdd can only be called via the interface and there's nothing that would cause an implicit conversion to be performed in this context


    Thank  you for the answer.
    • Edited by recherche Thursday, December 19, 2013 1:34 PM typo
    Thursday, December 19, 2013 5:27 AM