I have a base class and two derived classes:
public class base1
{
public int property1;
}
public class child1 : base1
{
public int property2;
}
public class child2 : base1
{
public int property3;
}
when I assign newProp
variable like this:
int i = 2;
base1 newProp = (i == 0 ? new child1
{
property1 = 1,
property2 = 3
} : null);
it works fine and the newProp
type changes to child1
class type,
but what I try to do is something like this:
int i = 2;
base1 newProp = (i == 0 ? new child1
{
property1 = 1,
property2 = 3
} : new child2
{
property1 = 4,
property3 = 6
});
but I get this error
Type of conditional expression cannot be determined because there is no implicit conversion between `class1` and `class2`
is there any way to do this?