Click here to Skip to main content
15,310,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I came across a topic named properties(get,set) to access a private variable of a class outside that class. But if we can set its access to public we can achieve same to use the variable outside that class. Then, why to use property concept any simple explanation and sample code, thanks.

What I have tried:

When I can do this by setting isGirl to public:
C#
class MainClass
    {
        class SubClass
        {
            public static bool isGirl;
          
        }
        static void Main()
        {
            SubClass.isGirl = true;
            Console.WriteLine(SubClass.isGirl);
        }
    }

In contrast, this is a property of the name variable which is quite lengthy. If public and property both do the same thing and also property must be within the same class where the variable is private, then why to use property?
C#
public bool IsGirl
          {
              get { return isGirl; }
              set { isGirl = value; }
          }
Posted
Updated 14-May-22 7:35am
v2

In simple cases like the above it is not critical to do it this way. But in more complex classes it may be that some code outside the class could store garbage in the object. Using getters and setters allows you to protect the object and always ensure that it only stores valid data.
   
Comments
Maciej Los 14-May-22 14:17pm
   
5ed!
One good reason is that properties are "variables with code" - if you use a public variable then the outside world can access or change the value of the variabel at any point - which means that if you want to make changes to your class you have to consider any possible effects on the outside world before you make them,. or you could break existing code.

For example, if you have a User class, with a public Name variable everything is fine. But in a months time, you need to reference the forenames and surname separately - so you create a FirstName and a Surname variable and delete the Name. And outside code which tries to access the Name fails!

If you used a Property, then when it is set, your code can break teh name up and store it in ForeName and Surname, then your code can assemble the full Name from the two parts on demand.
Existing code doesn't break, and your new requirement has been added.

Another advantage: some Controls can use Properties to display columns - for example, you can set a List<MyClass> as the DataSource for a DataGridView and it will display only the properties in separate columns and none of the fields.
   
v2
Comments
Maciej Los 14-May-22 14:17pm
   
5ed!
Fields should very rarely be public, but it may make sense some times.

In the particular case above, I recommend making the setter private:
public bool IsGirl { get ; private set ; }
You can't do that with a field.

Additionally, by using a Property, the setter can include validation or logging when such is desirable.

C#
public bool IsGirl
{
  get { return isGirl; }

  set 
  {
    LOG.Write ( "Changing value of IsGirl" , value ) ;

    isGirl = value;
  }
}
   
Comments
Maciej Los 14-May-22 14:18pm
   
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900