Tuesday, 15 March 2011

c# - Why I am getting the Unable to cast exception here -



c# - Why I am getting the Unable to cast exception here -

here code:

interface ia { } interface ic<t> { } class : ia { } class ca : ic<a> { } class programme { static void main(string[] args) { ia a; = (ia)new a(); // <~~~ no exception here ic<ia> ica; ica = (ic<ia>)(new ca()); // <~~~ runtime exception: unable cast object of type 'myapp.ca' type 'myapp.ic`1[myapp.ia]'. } }

why getting casting exception in lastly line of code ?

you need declare ic interface ic<out t> cast work. tells compiler ic<a> can assigned variable of type ic<ia>.

see, this page explanation.

c# oop

No comments:

Post a Comment