Properties

Properties are a convenient, built-in way to access private fields in C#.

You access properties the same way you would access a public field. Listing A.21 illustrates the use of properties to conceal private fields of the Point class.

Listing A.21. Properties in the Point Class
class Point{
   private double x=0;
   private double y=0;

   public Point(){
      x = 0;
      y = 0;
   }

   public Point(double X, double Y){
      x = X;
      y = Y;
   }

   public double X {
      get {  return x; }

      set {  x = value; }
   }

   public double Y{
      get{  return y;  }
      set {  y = value; }
   }

   public void print(){
      Console.WriteLine("(" + X + ", " + Y + ")" );
   }
}

Get Building e-Commerce Sites with the .NET Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.