[ACCEPTED]-Overriding (cast)-casting
Accepted answer
You must implement a explicit or implicit operator.
class Imp_A : AbsBase
{
public static explicit operator Imp_B(Imp_A a)
{
Imp_B b = new Imp_B();
// Do things with b
return b;
}
}
Now you 1 can do the following.
Imp_A a = new Imp_A();
Imp_B b = (Imp_B) a;
I'd suggest you write Imp_B like this, if 6 possible:
class Imp_B : Imp_A
{
private int A;
private int B;
private int C;
private int D;
private int lastE { get { return this.E.Last(); } }
}
If you can't actually derive from 5 ImpB, it's impossible for you to "treat" a 4 ImpA object as an ImpB transparently the 3 way you'd like, because they just aren't 2 the same thing in memory. So, implement 1 an explicit conversion.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.