You can use is
operator to check p is of Visitor
type or not.
The is operator checks if the runtime type of an expression result is
compatible with a given type.
foreach (Person p in pList)
{
if (p is Visitor visitorObj)
Console.WriteLine(visitorObj); //I believe you have overridden `ToString()` method
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…