What is the benefit of a bool property that just gets and sets a bool?
I know the benefit of only a get property or decisions that need to be made before you set a bool. I've seen examples using just get/set. I don't get it. What harm could making the bool variable public do?
(You can set it to null instead of a value, but you can do the same thing in a property if you don't check if value != null.)

public variables are not thread safe, the get and set properties make sure that a variable isn't in use by locking it temporarily an if is locked, causing a pause to occur until it is free. Its just the preferred way the framework handles variable access. with a simple bool value its easiest to do a Pubic Bool varname {get; set;} nothing complicated and it makes the system happy.

but if you want to use public variables that's up to you. Its obviously acceptable by the compiler, the code will run. in other languages every object is accessible from any other and there is no Public-Private mumbo jumbo.

>What harm could making the bool variable public do?

Here is a blog, explaining some differences.Properties vs. Public Variables

SUMMARY:

  • Reflection works differently on variables vs. properties, so if you rely on reflection, it's easier to use all properties.
  • You can't databind against a variable.
  • Changing a variable to a property is a breaking change.

Edited 6 Years Ago by __avd: n/a

>What harm could making the bool variable public do?

Here is a blog, explaining some differences.Properties vs. Public Variables

SUMMARY:

  • Reflection works differently on variables vs. properties, so if you rely on reflection, it's easier to use all properties.
  • You can't databind against a variable.
  • Changing a variable to a property is a breaking change.

Thank you. Very informative and useless information mixed indescriminatly. (Slight disagreement with the author, but the comments!)
"Automatic properties" - a feature added to C# I wasn't aware of and simplifies property implementation.
I thought of another reason. XML documentation. Any way to do that for a variable?

This question has already been answered. Start a new discussion instead.