So I ran across some code
IFirst x { get; private set; }
...
x = GetItem();
if (x == null) return;
ISecond y = x as ISecond; // this is the line I have issue with
GetItem defined at
public IFirst GetItem() {
//abridge
return new ConcreteA();
}
ConcreteA defined as
class ConcreteA : IFirst, ISecond {
}
The part where I am confused is when I downcast an item and return it from a function it seems that item does not loose its full identity. Meaning if I return ConcreateA as IFirst I would have expected it only to be an IFirst object.
But from the code in the first block it seems x, even though defined as IFirst explicitly can access its ISecond identity, and I assume its ConcreteA identity if need be?
Is this what is expected or am I missing a piece of code that is maintaining the conversion somewhere?
What is this behavior or pattern called in programming or C# specifically?
Aucun commentaire:
Enregistrer un commentaire