Yes, this solves your purpose but the story is different.
Fields with getters and setters are known as properties. If you don't set getters and setters explicitly then it's known as member variables or simply variables.
So say, if you code this...
public int a { get; set; }
private int b { get; set; }
These are known as properties.
Or, these are known as member variables.
public int a;
private int b;
Then also, it does the same thing and you can do the assignment and retrieval as like below in both the cases (provided you are not breaking access modifiers logic).
a = 1;
b = 2;
int c = a;
int d = b;
So, this has nothing to do with the approach. You can achieve this with both the approach.
Both the approach has nothing to do with public or private. They are known as Access Modifiers. Public properties / variables can be accessed from anywhere whereas the private properties / variables can be accessed from only within the block it's defined.
Well, then why properties have been introduced? What's the use? The answer is, they have been introduced to implement encapsulation.
Let's say, you have
private int _unitprice;
private int _quantity;
public int UnitPrice
{
get
{
return _unitprice;
}
set
{
if(value > 0) _unitprice = value
}
}
public int Quantity
{
get
{
return _quantity;
}
set
{
if(value > 0) _quantity = value
}
}
public int Total
{
get
{
return _unitprice * _quantity;
}
}
Say, you don't want users to access private members _unitprice and _quantity directly, instead access them using public facing properties with / without any validation. I did some validation in the example above, but it's not mandatory.
And some other properties like Quantity which doesn't exists say in table, but we have provided in Business Class for providing the facility and simplicity to the user.
Also, you may notice that Total have only get and no set. So you can even restrict assignment or retrieval using getters and setters.
Hope that helps :)