Question

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?

Was it helpful?

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().

OTHER TIPS

I recommend to you create a class converter to cast class A to class B.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top