0

I am having hard time understanding logic of properties , as far as i looked in internet its more logical and safe to use properties instead of public variables. But i want to understand the logic of properties to scratch it on my mind

private int myVar;

public int MyProperty
{
    get { return myVar; }
    set { myVar = value; }
}
public int myVar;

What are the benefits of using properties instead of public variables ? Why i shouldnt write a public variable in my class and use from instance?

  • https://stackoverflow.com/questions/737290/why-prefer-properties-to-public-variables – Ňɏssa Pøngjǣrdenlarp Apr 08 '20 at 22:40
  • In short, Property allows you restrict access to getter or setter with `private` modifier and execute some code inside it. – aepot Apr 08 '20 at 22:44
  • @aepot so inside set if i make this like if(myvar == 15) else if (myvar == 30) whenever i try to modify this myVar variable it will execute all if and elses inside of it ? – Little Anchovy Apr 08 '20 at 22:48
  • What are you exactly trying to do? please explain a bit more – curiousBoy Apr 08 '20 at 22:50
  • Yes, code will be executed as usual, and you may involve reserved word `value` in calculations for setter. – aepot Apr 08 '20 at 22:51
  • Based on what i pass to variable from another class, i want so that he would change somethings like integers ,strings without making a function for it , for an example if i pass 10 to myVar variable i want so that he would change to 15 himself without functions @curiousBoy – Little Anchovy Apr 08 '20 at 22:54
  • than you can use property and do that in your setter. BUT if you are executing code in a property, make sure you've written a property and not a method. A property should do less work-- a lot less work-- than a method. Properties should be lightweight. If your property incurs significant effort, it should be refactored into an explicit method. – curiousBoy Apr 08 '20 at 22:56
  • Btw, this question is overduplicated on StackOverflow. Please use search to learn more. – aepot Apr 08 '20 at 22:58

1 Answers1

1

Per your comment, you want to execute a code before you are setting the value. So simple example would be:

  class Example
    {
        DayOfWeek _day;
        public DayOfWeek Day
        {
            // whenever you try to get this value, it will run this block
            get
            {
                // We don't allow this to be used on Friday.
                if (this._day == DayOfWeek.Friday)
                {
                    throw new Exception("Invalid access");
                }
                return this._day;
            }
            // whenever you try to set its value, it will run this block
            set
            {
                // We dont allow to set if it is saturday
                if (value == DayOfWeek.Saturday)
                {
                    throw new Exception("Invalid access");
                } 
                this._day = value;
            }
        }
    }

Getters and setters basically let you execute "lightweight" code before you access/set the value.

BUT if you are executing code in a property, make sure you've written a property and not a method. A property should do less work-- a lot less work-- than a method. Properties should be lightweight. If your property incurs significant effort, it should be refactored into an explicit method. There are more to that.. Please read this post as there are some helpful content.

curiousBoy
  • 6,334
  • 5
  • 48
  • 56