-1

If Parent is parent class Which inherits Employee class then why

Person p = new Person();
Employee e1 = (Employee)p;

is not a compile time error? should not compiler figure it out at compile time.

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
ahsant
  • 1,003
  • 4
  • 17
  • 25
  • it is useful to be able to cast parent to child. this is possible because of some specific specialties in Intermediate language (IL) of Microsoft's .net.In compile time, basics of every class gets defined in IL before starting to compile "details" of classes. so there would be no Parenthood dependency in compile time. (unlike c++ that compile strictly follows the dependencies). – Sachamora May 29 '14 at 11:37
  • Did you mean `Person` instead of `Parent`? Did you also mean that `Employee` inherits from `Person`, and not the other way around? – dcastro May 29 '14 at 11:43
  • My original question was clear and without these issues, someone edited it and messed up the names. yes it was person, who was parent, and Employee was child. anyway... – ahsant May 29 '14 at 11:45

1 Answers1

2

Because it could be valid also. For example:

Person p = new Employee();
Employee e1 = (Employee)p;

Why would you expect compiler to produce error?

To make it more clear, lets assume casting from base class to derived class is not allowed. How'll you do the following then?

object o = new Employee();
Employee e1 = (Employee)o;

If compiler prevents you from casting at compile time you can't unbox any boxed struct, etc which is definitely needed.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • I understand the point of object 0 = new Employee(). however Person p = new Employee() is clearly saying Parent is holding a reference of child(or even if it was parent itself) and then down casting it to child is clearly wrong. if I were compiler this was not going to compile surely...thanks for your explanation anyway. I found this bug in one of our old codes, and thought compiler should have shouted... – ahsant May 29 '14 at 11:50
  • Compiler can't, Assume if `Person p` was a instance variable there is a chance that other thread can set a valid `Employee` before a cast to `Employee` isn't it? At compile time, compiler can't say it is wrong definitely. but for the local variable I think compiler can produce a warning atleast. – Sriram Sakthivel May 29 '14 at 11:54
  • @ahsant Given `Person p = new Person()`, the compiler does *not* know the type of the actual variable `p` points to. You're saying that the compiler *could* look at the variable declaration and say "Hey, `p` points to a `Person`, which cannot be casted to `Employee`!" - but that would be way more complex than you imagine. What if the declaration happened in an outter scope? – dcastro May 29 '14 at 11:58
  • Making the compiler detect that a local variable `p` can't be casted to `Employee`, by looking at its declaration a couple of lines above, isn't a feature worth implementing. The compiler helps you in many ways, but you can't expect it to hold your hand all the time. – dcastro May 29 '14 at 12:00
  • @dcastro but... am wondering why resharper also don't have such feature. Why this doesn't fall into static analysis? – Sriram Sakthivel May 29 '14 at 12:02
  • @SriramSakthivel resharper *could* do it but, again, maybe they never thought it was worth doing. Or maybe it was never proposed. Who knows? – dcastro May 29 '14 at 12:06