Your logic seems faulty to me, and regardless of the performace impact, calling an empty method smells of poor design.
In your situation, you have one interface which is Of Person
. You are stating that in order to be a person, you must be able to age, as enforced by your AgeAYear
method. However, according to the logic in your AgeAYear method (or lack thereof), an ImmortalPerson
cannot age, but can still be a Person
. Your logic contradicts itself. You could attack this problem a number of ways, however this is the first that pops into my head.
One way to accomplish this would be to set up two interfaces:
interface IPerson { void Walk(); }
interface IAgeable : IPerson { void AgeAYear();}
You can now clearly distinguish that you do not have to age to be a person, but in order to age, you must be a person. e.g.
class ImmortalPerson : IPerson
{
public void Walk()
{
// Do Something
}
}
class RegularPerson : IAgeable
{
public void AgeAYear()
{
// Age A Year
}
public void Walk()
{
// Walk
}
}
Thus, for your RegularPerson
, by implementing IsAgeable
, you also are required to implement IPerson
. For your ImmortalPerson
, you only need to implement IPerson
.
Then, you could do something like below, or a variation thereof:
List<IPerson> people = new List<IPerson>();
people.Add(new ImmortalPerson());
people.Add(new RegularPerson());
foreach (var person in people)
{
if (person is IAgeable)
{
((IAgeable)person).AgeAYear();
}
}
Given the setup above, your still enforcing that your classes must implement IPerson
to be considered a person, but can only be aged if they also implement IAgeable
.