1.11M Members

Get/Set - Why Use Properties/Methods?

 
2
 

As suggested by Duki in this thread I figured I'd compile all the workable examples and explanations here towards the question of "Why do we use get/ set to change properties?"

Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. C# properties enable this type of protection while also letting you access the property just like it was a field.
Becuase of get /set our class contains control over the input.
let say E.g. if you don't need to use get/set. you colud write function for setfunct or getfunct insted of using Property.
But that's a real problem like -- you'd rather write myc1.Y = 3; and be able to write things like myc1.Y += 5; , instead of myc1.SetY(myc1.GetY() + 5); //where myc1 is a object of class. So instead C# has properties.
Inside the setter, the keyword 'value' is the variable containing the value that is getting assigned to the property setnumberY.

Reason we use properties is it's a virtual.Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code.

It's basically just for data protection/hiding in the end.

By having get or set methods defined for a variable in a class you can set a variable as read only, write only, or read/write capable and the calling program need never directly access the variable directly.

Let's say, for example, you have a program where you want to be able to set a value for "y" but not read from "y" directly... You then wanted to have a process run on "y" and the result set to "x" which you want to be able to read, but not be able to directly write to. In the above example "y" is available to be "set" with the value you assigned, the method ApplesToOranges converts the value for "y" to a new value for "x" and "x" can be read but not directly written by anything outside the class. To anything outside of the class structure the only thing available to read is "Oranges" and the only available input is "Apples" and there is no further direct access to the underlying variables whatsoever.

I was just gonna say "Cos its good practice"..j/k
The use of properties (as clearly covered above) gives better control over how access is granted.
This is all part of Object Oriented practices..more specifically Encapsulation.

I've posted the code snippets in the order they're referred to by the above quote segments and labeled them accordingly with inline comments to try to clarify the concepts as much as I can while separating code from message as the snippet format requires here.

Hopefully this snippet submission will help future members with the question of why to use methods/properties for encapsulated data access :)

Class myclass // From pritesh2010's original post
{   
   int32 x,y;
    Public my(int32 x,int32 y)
      {
       this.x=x;
       this.y=y;
      }
     public int getnumberx()
      { return x; }
      public Void setnumberx (int x)
       { this.x=x; }
     public int getnumbery()
      { return y; }
      public Void setnumbery (int y)
       { this.y=y; }
}

Class myclass // From pritesh2010's original post
{   
   int32 x,y;
    Public my(int32 x,int32 y)
      {
       this.x=x;
       this.y=y;
      }
     public int getnumberx()
       get { return x; }
       set { x=Value; }
     public int getnumbery()
       get { return y; }
       set { y=value; }
}

class Point // Example 1 from Lusiphur's original post
{
    protected double x, y;
    public Point(double x, double y) { //Indirect access to x & y via public method "Point"
        this.x = x;
        this.y = y;
    }
    public double X {
        get { return x; } //Read Only access to protected/private variable "x" via public method "X"
    }
    public double Y {
        set { y = value; } //Write Only access to protected/private variable "y" via public method "Y"
    }
}

class YtoXClass // Example 2 from Lusiphur's original post
{
    private double x, y;

    public double Oranges {
        get {return x}
    }
    public double Apples {
        set {y = value;}
    }

    public void ApplesToOranges() {
        x = y * 4
    }
}

YtoXClass converter = new YtoXClass(); // Example 3 from Lusiphur's original post showing implementation of example 2
converter.Apples = 5;
converter.ApplesToOranges();
double Result = converter.Oranges;
Class myclass // From pritesh2010's original post
{   
   int32 x,y;
    Public my(int32 x,int32 y)
      {
       this.x=x;
       this.y=y;
      }
     public int getnumberx()
      { return x; }
      public Void setnumberx (int x)
       { this.x=x; }
     public int getnumbery()
      { return y; }
      public Void setnumbery (int y)
       { this.y=y; }
}

Class myclass // From pritesh2010's original post
{   
   int32 x,y;
    Public my(int32 x,int32 y)
      {
       this.x=x;
       this.y=y;
      }
     public int getnumberx()
       get { return x; }
       set { x=Value; }
     public int getnumbery()
       get { return y; }
       set { y=value; }
}

class Point // Example 1 from Lusiphur's original post
{
    protected double x, y;
    public Point(double x, double y) { //Indirect access to x & y via public method "Point"
        this.x = x;
        this.y = y;
    }
    public double X {
        get { return x; } //Read Only access to protected/private variable "x" via public method "X"
    }
    public double Y {
        set { y = value; } //Write Only access to protected/private variable "y" via public method "Y"
    }
}

class YtoXClass // Example 2 from Lusiphur's original post
{
    private double x, y;

    public double Oranges {
        get {return x}
    }
    public double Apples {
        set {y = value;}
    }

    public void ApplesToOranges() {
        x = y * 4
    }
}

YtoXClass converter = new YtoXClass(); // Example 3 from Lusiphur's original post showing implementation of example 2
converter.Apples = 5;
converter.ApplesToOranges();
double Result = converter.Oranges;
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This is an OP Kudos discussion and contributors may be rewarded
Post:
Start New Discussion
View similar articles that have also been tagged: