I have several types of object instances that inherit from a common interface. I would like to access common methods from each object, iterating through a list or arraylist or collection. how to do it?
{ interface ICommon { string getName(); } class Animal : ICommon { public string getName() { return myName; } } class Students : ICommon { public string getName() { return myName; } } class School : ICommon { public string getName() { return myName; } } }
When I add an animal, student and school to object [] and try to access in type loop
for (loop) { object[n].getName // getName is not possible here. //This is what I would like to have. or a = object[n]; a.getName // this is also not working. }
Can I access a common method of different types from a list or collections?
source share