0

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 ?

Jacob Lambert
  • 7,449
  • 8
  • 27
  • 47
gema
  • 523
  • 1
  • 7
  • 17

4 Answers4

2

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.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
1

From design perspective the unit price property is exactly the same as the other properties but because there is a constraint on it.

 if (value >=0)

only positive prices are valid, you have no other option than to foresee a private variable whereon the property is based.

In earlier versions of .NET we could not do something like this :

public int Id { get; set; }

And had to write it out in full all the time. The notation above is more handy and it makes the code clearer too. It used to be like this :

private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}
Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
0

The public property UnitPrice is exposed to other classes to be read or modified. But being a property allows your class a degree of control over the value - you can validate any change for instance. Also, internally you can completely change how the value is obtained and modified without changing the contract of the class. So in other words, you would be able to make such changes without affect any consumers.

The backing field unitPrice is purely an implementation detail, it is internal state that is encapsulated by the class. Exposing this field means you lose any and all chance to modify the derivation of the value and you have no entry point for validating changes to the value. Properties may seem pointless abstraction at first but they will help make your code more maintainable as changes can be more easily confined to single classes.

The get and set blocks are purely syntactic sugar and under the covers the compiler creates two methods called get_UnitPrice() and set_UnitPrice(x) that are used for read/write operations on the property. You could just use similar methods but properties are more readable and designed to be a low cost way of interacting with class state and methods are for providing behaviour.

James Lucas
  • 2,452
  • 10
  • 15
0

Yes, get and set properties are useful when you want to have some control over variable. Consider the following case.

If you specify the variable as private you want no one to be able to access your variable.

But if you want others to be able to access your private variable but you dont want others to change it. In this case you can use Properties

public class Product
{
 private decimal price;
 public decimal Price{get{ return price;}}
}

now others have access to the price but they cant change it

In your case you are allowing others both to get and set your unitprice, which is equal to giving unitprice public access. However you are allowing on one condition that the unitprice should be set to 0. So C# properties are perfect match for this scenario.

Hope this helps!!

Umamaheswaran
  • 3,690
  • 3
  • 29
  • 56