In Program, i'm writing on console the breed of a Dog.
Which is the best approach and why?
I suppose that the first way does less operations, but to do it, i have to declare the variable as public.
Is maybe a better choice to declare it private and return the data from a method like i did in the second writeline?
I would like to understand which kind of approach is the best considering every important aspect of a software
class Program
{
static void Main(string[] args)
{
Dog fuffy = new Dog("Fuffy", "Armant");
Console.WriteLine(fuffy.breed);
Console.WriteLine(fuffy.getBreed());
}
}
class Dog
{
public string name;
public string breed;
public Dog(string name, string breed)
{
this.name = name;
this.breed = breed;
}
public string getBreed()
{
return this.breed;
}
}
Edit:
Is there a real difference between using the getter and this method?
Isn't the getter just an "hidden" way to write and execute that method?
Is a getter giving a better prestation compared to the method?
class Dog
{
public string name { get; }
public string breed { get; }
public Dog(string name, string breed)
{
this.name = name;
this.breed = breed;
}
public string getBreed()
{
return this.breed;
}
}