There are two classes. Class B dervied from A.
class A
{ }
class B : A
{
public B()
{
}
public int Number { get; private set; }
}
This gives me the error ‘unable to cast from A to B’.
void Test()
{
var a = new A();
var b = (B)a; // <== unable to cast.
}
How can I cast the object in variable A to class B?
Thank you.
Your code fails because you don’t have a B.
You could do it if you created a B instead of an A:
You could think of A as
Animaland B asBear.You can always say a bear is an animal, but if all you’ve got is an unknown animal you can’t always cast it into a bear. That works only if it actually is a bear.