Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top