0

I was trying to help my friend to understand things such as fields and properties, and getters/setters. He then used properties without a private field, and told me it worked. I never even knew this was possible and can't seem to find too much about it online.

As an example:

public int Number { get; set; }

Instead of:

private int number;
public int Number
{
   get { return number; }
   set { number = value; }
}

It seems to work (as far I can see), but now I have the following questions:

  • What is happening behind the scenes?
  • What way is prefered?
  • Any pros/cons?

EDIT:

I always thought { get; set; } was the same as get { return x; } set { x = value; }. Oh well, good that I now know it. The question however remains the same. Is one considered better than the other? Pros/Cons?

anthonytimmers
  • 258
  • 1
  • 8
  • Exactly the way you have the second code block, `private int number` is completely different from `public int Number { get; set; }`. – hunch_hunch Sep 18 '14 at 19:54
  • 2
    You might be thinking of having `private int number;` and also having `public int Number { get { return number; } set { number = value; } }` – hunch_hunch Sep 18 '14 at 19:56
  • 3
    I would recommend reading up on Auto Properties and Getters & Setters.. for yourself as well as your "Friend" – MethodMan Sep 18 '14 at 19:59
  • @hunch_hunch I seemed to indeed have switched them up. Changed it now. – anthonytimmers Sep 18 '14 at 20:04
  • 2
    [Auto-Implemented Properties](http://msdn.microsoft.com/en-us/library/bb384054.aspx): `the compiler creates a private, anonymous backing field` – Jonesopolis Sep 18 '14 at 20:05
  • @Jonesy Alright, and does this have any pros/cons compared to the other way? If so, which? What is considered best practice? – anthonytimmers Sep 18 '14 at 20:08
  • 1
    To answer your trailing question (my previous comment/link was wrong copy/paste- sry) http://stackoverflow.com/questions/8116951/any-reason-to-use-auto-implemented-properties-over-manual-implemented-properties – Nyra Sep 18 '14 at 20:20
  • @alykins Thanks, gonna read that now. Most confusion came because of school using the auto-implement property even though they defined private fields (which explain why I got the 'x is declared but never used' warnings. – anthonytimmers Sep 18 '14 at 20:23

1 Answers1

1

In the first case the compiler is automatically creating a private field as the backing store for the property, giving it a name not accessible in user code, and creating methods to get and set its value.

In you're code you're taking the time to do it explicitly, and also creating a valid identifier in the scope of the class for that backing field, unlike the previous example.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • It is never used anywhere because it's just that single piece of code in a class. I could edit it so that it gets used somewhere, but that's not really related to the question as it's about properties + private fields vs just properties and not the code surrounding it. – anthonytimmers Sep 18 '14 at 19:58
  • 1
    @anthonytimmers Well in the code you showed you're creating two different fields, you're not creating a property with an explicit backing store. As I said, setting the property won't set that field, and getting the value of the property isn't getting the value of the field. You have two fields there. If any other code accesses the field (not the property) then it won't affect or be affected by the code using the property, and vice versa. – Servy Sep 18 '14 at 19:59
  • Gonna accept this as the answer. Just wondering, so the only con to using auto-implemented properties would be that you can't specify what your getters and setters need to read/write? – anthonytimmers Sep 18 '14 at 20:21
  • 1
    @anthonytimmers If you need to do something in a getter or setter that is different from what the automatically generated properties automatically generates, then you can't use an automatically generated property; that is correct. – Servy Sep 18 '14 at 20:22
  • Alright, thanks for clearing that up and your answer. And again, sorry for the confusion. – anthonytimmers Sep 18 '14 at 20:25