1

Need to understand the benefit of using automatic properties apart from less line of codes?

Intially we used as below :

private int sample; public int Sample { get {return sample}; set {this.sample=value}; } 

Now we just get n set it directly.Why we used to define a private variable?

4
  • Possible duplicate: http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0/653799#653799 Commented Jul 2, 2014 at 6:49
  • In terms of writing your properties, how many times have you accidentally wrote get{return Sample;}? Commented Jul 2, 2014 at 6:51
  • @Sayse: Resharper complains if you do that. Commented Jul 2, 2014 at 18:05
  • @Brian - I don't use resharper, and didn't even know about it when I started learning C# (when I was making this mistake more often). Its only on a slow day this ever happens now, I wrote it as a reason to prefer auto's Commented Jul 3, 2014 at 6:42

2 Answers 2

5

You're still creating a private variable - it's just done for you behind the scenes by the compiler. The variable is given an "unspeakable name", ensuring you can't refer to it in source code.

You're still getting all the benefits of properties (you can later change from an automatic property to a "manual" property, with no compatibility issues) but without all the cruft. The benefit is just that the code ends up being a lot more concise. I regard that as a significant benefit though :)

Sign up to request clarification or add additional context in comments.

Comments

1

Your private sample variable is called a backing field, and it holds the actual data for the property. If the property is only basic get/set, there is no need declare a backing field yourself. As Jon Skeet mentioned the backing field will be generated by the compiler behind the scenes. If you requirements change, you can always decide later to declare a backing field yourself, and use that one in your property. Since the rest of your code uses that property, your code will still compile.

When your property contains some logic, the backing field is very usefull.

For example the following can't be done without a backing field ( not within the setter i mean)

public int Sample { get { return _sample; } set { if (value > _sample) _sample = value; } } 

Also, your property could be written like this if the getter and setter have no logic.

public int Sample { get; set; } 

8 Comments

Isn't your example the exact opposite of the question, proving a benefit to "manual" properties?
I thought the poster would like to have an answer to the following question "Why we used to define a private variable?"
Ah fair enough, didn't see that bit!
" If the property is only basic get/set, there is no need for a backingfield." Please help on this statement. In case of basic get/set where the actual data is holded?
@Meg See Jon Skeet's answer for this: If no backingfield is explicitly defined, the compiler creates one behind the scenes
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.