HI why do we use get/ set to change properties?

I mean instead of writing

class Point {
    double x, y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double X {
        get { return x; }
        set { x = value; }
    }
    public double Y {
        get { return y; }
        set { y = value; }
    }
}

cant we write simply,
supposse for eg,

x=5.0;
y=x;

please explain why do we need to use this get/set methoss?

Recommended Answers

All 6 Replies

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.

Class myclass
{   
   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; }
}

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:

Class myclass
{   
   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; }
}

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.

commented: well written example +1
commented: Excellent detail +7

Well dayum... I was about to go and give a big well-worded example of the use of methods vs direct data access and then I read the 1 reply and saw that pritesh had beat me to it :twisted:

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.

Edit: A more fitting example for what I've described of the use of methods would be as follows

class Point {
    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"
    }
}

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. You might approach that as follows:

class YtoXClass {
    private double x, y;

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

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

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. This example (though not very realistic) would be used in a way similar to this (within the main program and not within the class itself):

YtoXClass converter = new YtoXClass();
converter.Apples = 5;
converter.ApplesToOranges();
double Result = converter.Oranges;

Hope that helps :)

commented: Good explanation (at 4 am?) +7
commented: good explaination +2
commented: Great examples +7

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.

commented: The nail right on the head! +7

I'm a big fan of how I said I wasn't going to post a long detailed explanation then did >.> Waking up at 4am for the LOSS!! :twisted:

Just skimming through posts here - really think someone should write a code snippet with all of this in it. Very good material and explanations here!

Just skimming through posts here - really think someone should write a code snippet with all of this in it. Very good material and explanations here!

Done :twisted:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.