I think you should be able to use the "is" statement to get this done.
Example:
private Room FamRoom() { IList<FamilyInstance> fams = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).Cast<FamilyInstance>().ToList(); if (fams[12] is FamilyInstance famIst) { Room room = famIst.Room; return room; } return null; }