7

Take private string Property {get; set;} versus private string field.

Note that both are private (so they will not be exposed outside of this class) and that the property is not employing extra validation.

As regards semantics, do they have different meanings? In the sense that, are they interchangeable when used like this?

And when it comes to implications, such as (micro?) performance, does it matter if you create a field versus a property i.e. letting the compiler take care of the backing field for you.

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • Very similar: [Using a private auto property instead of a simple variable for a programming standard](http://stackoverflow.com/questions/6125580/) – H H May 27 '11 at 16:08

5 Answers5

3

When they're private the only difference I know is that the property is not suitable for out and ref parameters.

But mostly a private property does not deliver any advantages (over a field), so why bother?
There probably are (micro) performance costs. I would worry more about the extra clutter.

H H
  • 263,252
  • 30
  • 330
  • 514
3
  • A property is about data hiding of a field
  • A private property does not mean much, since whoever has access to the property, will have access to the field as well
  • There is no performance implication for auto-property versus backing field since compiler spits out the backing field but there can be serialisation/deserialisation caveats.

UPDATE

Performance implications:

There is a slight performance in using property (auto or with backing field) vs field since a property is a method and a CLR virtcall needs to be called.

But as I said, there is not much point in using property and I believe a field is more readable as usually immediately visible by the naming convention (starting with underscore or camel casing).

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Does a private property make sense since they are most likely used to access a private field? – Xaisoft May 27 '11 at 16:05
  • There is NO performance implications for autoproperties when you compile 'release' -> without trace and with optimization. – MajesticRa May 27 '11 at 16:31
1

You can't have a reference to a property, but you can get a reference to a member. Thus, if you use members, you might have trouble switching them to properties for whatever reason later, such as adding the notorious validation.

vines
  • 5,160
  • 1
  • 27
  • 49
  • I was thinking about this but would it be wise to say that because of this reason, to always use properties instead of private members? – Andreas Grech May 27 '11 at 16:09
  • @Andreas Grech : It depends, I suppose. There are cases when a member is definetely enough, such as some temporary/helper/flag/whatever value, and there are cases where I'd consider properties too. – vines May 27 '11 at 16:13
  • If the cleanest way of coding something would require getting a `ref` to a member (e.g. so it can be given to `Interlocked` methods), using a property might force the use of a clunkier design. If for whatever reasons the use of a property will end up being ultimately unavoidable, then it may be better to start out using the clunky design than implement the system with a clean design and then have to replace it with a clunky one when the field is replaced with a property. On the other hand, if the member will never *need* to be a property, it would be better to use the cleaner design. – supercat Apr 02 '13 at 20:37
0

Creating a private automatic property has no use that I can see. If its not automatic it could be used as some kind of internal 'event handler' to keep the object state up to date: perform some actions every time the field changes (through the setter) anywhere in the code.

Performance? I dont think there would be an issue, not even at a micro micro level.

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • 'has no use that I can see' you indeed are not correct. See my post why. – MajesticRa May 27 '11 at 17:13
  • @MajesticRa: I see you haven't read my answer completely. I find **automatic** private properties useless because you can't add any custom logic to them. Indeed, as I mention in my answer, the only use I can find for private properties is to centralize some state logic of your class, but then they can't be automatic as the OP implied. – InBetween May 27 '11 at 17:16
0

Properties ARE functions.
Fields are 'variables with at least class visibility'.

So if you have private property vs private field:


The difference from the performance point:

no difference if you use optimization and no trace (properties are treated as inlines).

The difference in semantics:

1) Formally no difference.
2) More deeply, there IS a difference. Since properties are functions you CAN get a delegate from getter and setter. And CAN use the delegate as... delegate, like putting this delegate to the list with other delegates (Create a delegate from a property getter or setter method)

The difference from a design view:

But properties are functions that looks like variables. Why one could need functions that look like variables?

Lets say you have class Hand and this hand has variable fingersNumber.

 class Hand
 {
     public int fingersNumber;
 }

Then you may have a lot of code like

 if(he is BadPerson) leftHand.fingersNumber--
 if(doctor.Heal()) leftHand.fingersNumber++

But at some point you may want to add some other variable to Hand. Lets say it is ringsNumber. And you know, that you can't have more than 10 rings for each finger.

 class Hand
 {
     public int fingersNumber;
     public int ringsNumber;

 }

now, you cannot just do

 leftHand.fingersNumber-- 

because you have to control ringsNumber on fingersNumber dependence.

So you have to create some functions that whould check this dependance. Also you have to hide fingersNumber ander ringsNumber from users so they cannot change this fields without the check.

 class Hand
 {
     private int fingersNumber;
     private int ringsNumber; 
     public int GetFingersNumber(){...check logic...}
     public void SetFingersNumber(int value){...check logic...}
     public int GetRingsNumber(){...check logic...}
     public void SetRingsNumber(int value){...check logic...}
 }

And use this functions as

 if(he is BadPerson) leftHand.SetFingersNumber(leftHand.GetFingersNumber()-1)

The problem here that the old code leftHand.fingersNumber-- would not work now. And from the beginning you wouldn't know that one will add rings in the future. To solve the problems it became a paradigm to set fields as private and use Set and Get functions to get and change variables and BE SURE that in the future one could add any logic there and code would work!

Setters and Getters is a current situation in C++, Java and many languages. But C# creators went further and decorated such getters and setters functions as "properties".

 class Hand
 {
     private int fingersNumber; 
     public int FingersNumber
     {
          get{return fingersNumber;}
          set{fingersNumber=value;}
     }
     ...     
  }
  ...
  if(he is BadPerson) leftHand.FingersNumber--;

But most of the time people create such simple property and, you see the example, it is 5 lines of routine code. So at some version of C# autoproperties was added to simplify life of programmers. So your class is probably looks like

 class Hand
 {
     public int FingersNumber{get;set;}
 }

but at any time you can extend this get set behaviour:

  class Hand
 {
     private int fingersNumber; 
     public int FingersNumber
     {
          get{...check logic...}
          set{...check logic...}
     }

     ...     
  }

And it will NOT BRAKE ANY CODE. Like

  if(he is BadPerson) leftHand.FingersNumber--;

So thats what are the properties, why do they used and what is the difference with fields.

Also as I ser earlier, simple properties and autoproperties have the same performance as variables if you use optimization. Se disassembly or just google about it.

Community
  • 1
  • 1
MajesticRa
  • 13,770
  • 12
  • 63
  • 77
  • I appreciate your lengthy answer, but my question wasn't really about properties and fields in general. I was asking whether there's a semantic difference between using a private property vs using a private field and I specifically said that the property is not employing extra validation. – Andreas Grech May 27 '11 at 18:05
  • Formally there is no difference. If you see disassembly, if you use optimization and run without trace, autoproperties will be treat as inline autogenerated fields. So there is no micro-performance difference. – MajesticRa May 27 '11 at 19:01
  • BUT properties let you ADD validation or memory barriers on the further stages of development. Fields do not. That is the major. – MajesticRa May 27 '11 at 19:02
  • I know, I'm not disputing you. I knew what you said about properties before I asked this, and my question is not related to that at all. So, apart from the validation, what's the major difference between a private field and a private property? – Andreas Grech May 27 '11 at 20:16