Friday, December 21, 2012

Why use get set properties in C#

We use get and set properties basically for the encapsulation purpose. There are scenarios that we need to hide object's fields from others. These fields can be resulted by some other multiple fields or can be be field data with some changes.

public class DataClass
  private string myval = "1";
  public int MyNumber
    get { return Convert.ToInt32(myval) + 2; }
    set { myval = value.ToString(); }

protected void Page_Load(object sender, EventArgs e)
  DataClass ds = new DataClass();


In the above example internal operation with the myNumber is hidden. Others will only see the out put result.

No comments:
Write comments
Recommended Posts × +