I have a demo script which shows the question quite nicely so I will show the script, then ask.
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog() { Age = 4 };
//This approach (version A)
dog.DisplayDogYears();
//Or this approach (version B)
displayDogYears(dog);
Console.ReadKey();
}
private static void displayDogYears(Dog d)
{
Console.WriteLine("The dog is {0} years old in human years.", d.Age * 7);
}
}
public class Dog
{
public int Age { get; set; }
public void DisplayDogYears()
{
Console.WriteLine("The dog is {0} years old in human years.", Age * 7);
}
}
There are 2 approaches which do the same thing (approach A and B). Are there any reasons to prefer one approach over another, in terms of scalability and readable code. I can see one advantage of the approach A is that this method is available to all instances of the Dog class (so nice and scalable and reusable and maintainable, however, when debugging it does mean I have to move between different classes (and VS has to jump to different files etc) instead of viewing everything in just one file.
Any thoughts?
Dave
I would prefer to have a method in the class Dog:
and call this method from the main with a format string you like.