Does type casting in C# occur here?
-
07-04-2021 - |
Frage
I have a basic question regarding type casting.
class A { }
class B : A { }
B b = new B();
A a = (A)b;
In the above code whether type casting will occur?
interface IA
{
void process();
}
class B : IA
{
#region IA Members
void IA.process()
{
throw new NotImplementedException();
}
#endregion
public void process() { }
}
B b = new B();
b.process();
((IA)b).process();
In the above code whether type casting will occur?
Lösung
You are using explicit cast like (A)b in both cases. So type cast will happen in both cases. If explicit casting is removed, then in first case implicit cast will occur and second case will not have any casting as it is same as b.process().
Andere Tipps
I recommend to you create a class converter to cast class A to class B.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow