Search code examples
c#interfacecastingexplicit-interface

Does type casting in C# occur here?


I have a basic question regarding type casting.

class A { }
class B : A { }

B b = new B();
A a = (A)b;

In the above code whether type casting will occur?

    interface IA
    {
        void process();
    }

    class B : IA
    {
        #region IA Members

        void IA.process()
        {
            throw new NotImplementedException();
        }

        #endregion
        public void process() { }
    }

    B b = new B();
    b.process();
    ((IA)b).process();

In the above code whether type casting will occur?


Solution

  • You are using explicit cast like (A)b in both cases. So type cast will happen in both cases. If explicit casting is removed, then in first case implicit cast will occur and second case will not have any casting as it is same as b.process().