X hits on this document

823 views

0 shares

0 downloads

0 comments

247 / 396

C# LANGUAGE SPECIFICATION

Unlike public fields, properties provide a separation between an object’s internal state and its public interface. Consider the example:

class Label { private int x, y; private string caption;

public Label(int x, int y, string caption) { this.x = x; this.y = y; this.caption = caption; }

public int X { get { return x; } }

public int Y { get { return y; } }

public Point Location { get { return new Point(x, y); } }

public string Caption { get { return caption; } } }

Here, the Label class uses two int fields, x and y, to store its location. The location is publicly exposed both as an X and a Y property and as a Location property of type Point. If, in a future version of Label, it becomes more convenient to store the location as a Point internally, the change can be made without affecting the public interface of the class:

class Label { private Point location; private string caption;

public Label(int x, int y, string caption) { this.location = new Point(x, y); this.caption = caption; }

public int X { get { return location.x; } }

public int Y { get { return location.y; } }

public Point Location { get { return location; } }

234Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views823
Page views823
Page last viewedSun Dec 04 11:11:44 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments