2

What is the benefit of using get and set properties in the following example?:

class Program
{
    public class MyClass
    {
        private int age;

        public int persons_age
        {
            get
            {
                return age;
            }

            set
            {
                age = value;
            }
        }

    }

    static void Main(string[] args)
    {

       MyClass homer = new MyClass();

       homer.persons_age = 45; //uses the set property

       homer.persons_age = 56; //overwrites the value set by the line above to 56

       int homersage=homer.persons_age; //uses the get property

       Console.WriteLine(homersage);

    }
}

what is the difference between doing that and the following?:

public class MyClass
    {
        public int age;
    }

 static void Main(string[] args)

    {

       MyClass homer = new MyClass();

       homer.age = 45;

       homer.age = 56; //overwrites the value set by the line above to 56

       int homersage=homer.age;

       Console.WriteLine(homersage);

    }

What is the benefit of using the get and set property when there is no difference at all in what the above two programs do? Unlike the scenario where the client has limited ability to assign vales to the field via the set method through some logic checking, in this situation I don't see any functional difference between the two programs show here.

Also, some programming books use the phrase "...breaks the client code" if such properties are not used for class fields. Can someone explain this?

Thanks.

  • Thanks for the suggestion. I did try using a similar search phrase but could not get relevant results. Thanks. I appreciate it. :) – user3444406 Mar 21 '14 at 00:41
  • 2
    See also [C# : Auto-properties with or without backing field - preference?](http://stackoverflow.com/questions/9645938/c-sharp-auto-properties-with-or-without-backing-field-preference). If you create a property `public int age { get; set; }`, then that's the difference with your first example. – CodeCaster Mar 21 '14 at 00:47
  • You can also use auto-property syntax, which has short declaration like fields, and is a property at the same time. The only practical difference with a field is that it cannot be passed as `ref` and `out` argument. – Dmitry Mar 21 '14 at 00:50

1 Answers1

1

One answer I can think of is that with setter and getter methods, you can add checking or validation in it. unlike accessing it directly by declaring it public, the checking/validation will fall out on a different location and a possible duplication of codes. example:

    public class MyClass
    {
        private int age;

        public int persons_age
        {
            get
            {
                return age;
            }

            set
            {
                if(value > 0)
                    age = value;
                else
                    //do something here
            }
        }

    } 

this way you are defining constraints on your objects.

KiX Ortillan
  • 210
  • 2
  • 13