Just want to make sure.
public class Product { private decimal unitPrice; public int Id { get; set; } public string Code { get; set; } public string Name { get; set; } //private string code; public decimal Unitprice { get { return unitPrice; } set { if (value >=0) unitPrice = value; } } }
Why we would have to make private variable to unitPrice to return the value to UnitPrice, does it written for some reasons ?
You dont make it private
just to return the value for it. private
is one of the access modifier
here. They are used to limit the scope of the access/usage of that particular variable in your code.
private
here means that unitPrice
is currently accessible or can be used by this particular class only. No other outside assembly can use this variable.
If you want to access a variable outside in other areas, you can opt to make it public
.
Hope this clears it.