Can anyone tell me how to case a base class into a derived class like in the example below. Here I am receiving an exception when the person object is cast as an employee.
Code:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.firstName = "egar";
Employee e = (Employee)p;
}
}
public class Employee : Person
{
private string _dept;
public Employee()
{
_dept = "";
}
public string dept
{
get
{
return _dept;
}
set
{
_dept = value;
}
}
}
public class Person
{
private string _firstName;
public Person()
{
_firstName = "";
}
public string firstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
}
}