I have the following code:
namespace ConsoleApplication2
{
public class Test1
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public Test1(string name,string lastName,int age)
{
FirstName = name;
LastName = lastName;
Age = age;
}
//Error: user-defined conversions to or from a derived class are not allowed
public static implicit operator Test2(Test1 d)
{
return new Test2(d.FirstName,"MiddleName" , d.LastName, d.Age);
}
}
public class Test2 : Test1
{
public string MiddleName { get; set; }
public Test2(string name,string middleName, string lastName, int age) : base(name, lastName, age)
{
MiddleName = middleName;
}
}
}
I get error about conversion to derived class. But I don't understand why is the conversion to a derived class not allowed ??